Chapter 10 Efficient Binary Search Trees

Slides:



Advertisements
Similar presentations
Splay Trees Binary search trees.
Advertisements

AVL Trees binary tree for every node x, define its balance factor
Lecture 9 : Balanced Search Trees Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
1 AVL Trees. 2 AVL Tree AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children.
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
Trees Types and Operations
CS202 - Fundamental Structures of Computer Science II
4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.
CS Data Structures Chapter 10 Search Structures (Selected Topics)
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
Dynamic Dictionaries Primary Operations:  Get(key) => search  Insert(key, element) => insert  Delete(key) => delete Additional operations:  Ascend()
Red Black Trees Colored Nodes Definition Binary search tree.
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
AVL Trees ITCS6114 Algorithms and Data Structures.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
AVL Trees v z. 2 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
CS Data Structures Chapter 10 Search Structures.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
COSC 2P03 Week 51 Representation of an AVL Node class AVLNode { AVLnode left; AVLnode right; int height; int height(AVLNode T) { return T == null? -1 :
M-ary Trees. m-ary trees Some trees need to be searched efficiently, but have more than two children l parse trees l game trees l genealogical trees,
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
Data Structures Using Java1 Chapter 10 Binary Trees.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
Chapter 05 Trees (Part II). Array Representation We can use an array to represent a complete binary tree. Lemma 5.4 ▫If a complete binary tree with.
AVL Trees CSE, POSTECH.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
AVL Trees binary tree for every node x, define its balance factor
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
Chapter 29 AVL Trees.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Cinda Heeren / Geoffrey Tien
Data Structures Using C++ 2E
Chapter 20: Binary Trees.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
Chapter 21: Binary Trees.
AVL Trees CENG 213 Data Structures.
AVL Trees: AVL Trees: Balanced binary search tree
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
AVL Search Tree put(9)
Dynamic Dictionaries Primary Operations: Additional operations:
Self-Balancing Search Trees
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
AVL Tree By Rajanikanth B.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
AVL Trees Dynamic tables may also be maintained as binary search trees. Depending on the order of the symbols putting into the table, the resulting binary.
INSERT THE TITLE OF YOUR PRESENTATION HERE AVL TREE.
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

Chapter 10 Efficient Binary Search Trees Part I – AVL Trees

Unbalanced Binary Search Tree Number of comparisons needed to search for NOV: 6. Average number of comparisons: 3.5 5 4 8 1 2 3 7 6 9 12 11 10

Skew Binary Search Tree Consider the keys are entered in lexicographic order. In worst case, searching a skew binary tree corresponds to sequential search in a ordered linked list. 1 2 3 4 5 6 7 8

Binary Search Tree Consider a balanced binary search tree as illustrated. Number of comparisons needed to search for NOV: 6. Average number of comparisons: 3.1 6 4 9 2 1 5 8 3 11 12 10 7

Binary Search Trees: Balanced vs. Unbalanced The average and maximum search time can be minimized if the binary search tree is maintained as a complete binary tree all the times. The time becomes O(log n) for an n-node binary search tree. AVL tree (1962) Balanced binary search tree with respect to the heights of subtrees. Any retrievals can be performed in O(log n) time. The resulting tree of insertion or a deletion remains height-balanced.

Height-Balanced Definition An empty tree is height-balanced. If T is nonempty binary tree with TL and TR as its left and right subtrees respectively. T is height-balanced iff TL and TR are height-balanced, and |hL-hR|≦1 where hL and hR are heights of TL and TR, respectively.

Examples Not height-balanced Height-balanced 5 4 8 1 2 3 7 6 9 12 11 10 6 4 9 2 1 5 8 3 11 12 10 7 Not height-balanced Height-balanced

Balance Factor Definition: For every node T, define its balance factor, BF(T), as BF(T) = hL - hR where hL and hR are the heights of the left and right subtrees of T. BF(T) for any node T in an AVL tree is –1, 0, or 1.

Balance Factors for an AVL Tree 1 -1

Construction of an AVL Tree Consider to insert the following numbers: 8, 9, 10, 2, 1, 5, 3, 6, 4, 7, 11, 12 -1 8 8 9 Insert 8 Insert 9

Consider the nearest parent A with bf = ±2 -2 8 8 10 8 9 -1 RR 9 9 insert in the right subtree of the right subtree of A 10 Insert 10

1 9 1 8 10 2 Insert 2

Consider the nearest parent A with bf = ±2 9 10 2 9 1 8 2 8 8 10 1 LL 2 2 1 insert in the left subtree of the left subtree of A Insert 1

Consider the nearest parent A with bf = ±2 9 -1 2 8 1 10 5 2 9 9 -1 LR 2 10 1 1 8 8 insert in the right subtree of the left subtree of A 5 Insert 5

1 1 8 8 -1 -1 -1 -1 2 9 2 9 1 1 1 5 10 5 10 3 3 6 Insert 3 Insert 6

Consider the nearest parent A with bf = ±2 9 -1 2 1 8 10 5 3 6 4 2 8 -2 -1 2 2 9 1 RL 1 5 10 -1 3 3 6 insert in the left subtree of the right subtree of A 4 Insert 4

2 9 -1 2 1 8 10 5 3 6 4 7 8 8 -1 -1 3 9 LR 1 -1 2 5 5 10 -1 1 4 6 7 Insert 7

-1 5 -1 1 3 8 -2 1 -1 RR 9 9 2 4 6 -1 1 7 10 10 10 2 1 8 9 5 3 6 -1 4 7 11 11 Insert 11

-1 5 1 -1 3 8 1 -1 -1 10 2 4 6 -1 1 7 9 11 12 Insert 12

Rotation Types (1) Suppose Y is the new node. LL: Y is inserted in the left subtree of the left subtree of A. A B B LL C A TA C TB TB TA

Rotation Types (2) LR: Y is inserted in the right subtree of the left subtree of A A C LR TA B B A C TCL TCR TA TCL TCR

Rotation Types (3) RR: Y is inserted in the right subtree of the right subtree of A. A B RR B TA A C C TB TA TB

Rotation Types (4) RL: Y is inserted in the left subtree of the right subtree of A C A RL A B TA B C TA TCL TCR TCL TCR

The Class Definition of AVL Tree class AvlNode { friend class AVL; public: AvlNode(int k) {data = k; bf = 0; leftChild = NULL; rightChild = NULL; } private: int data; int bf; AvlNode *leftChild, *rightChild; }; class AVL { AVL() : root(0) {}; bool Search(int key); bool Insert(int key); bool Delete(int key); AvlNode *root; Store the value of balance factor of the node

Phase 1 bool AVL::Insert(int key) { if (!root) root = new AvlNode(key); return true; } //Phase 1: locate insertion point for key AvlNode *a = 0, //most recent node with bf = ± 1 *pa = 0, //parent of a *p = root, //p moves through the tree *pp = 0; //parent of p

while (p) { if (p->bf != 0) a = p; pa = pp; } if (k > p->key) pp = p; p = p->rightChild; else if (k < p->key) p = p->leftChild; else return false;

Phase 2 //Phase 2: Insert and rebalance //k is not in the tree and may be inserted as the appropriate child of pp. AvlNode *y = new AvlNode(k); if (k < pp->key) pp->leftChild = y; //insert as left Child else pp->rightChild = y; //insert as right Child

Adjust balance factors of nodes on path from a to pp. int d; AvlNode *b, //child of a *c; //child of b if (a == NULL) { p = root; d = (k > p->key)? -1 : 1; } else if (k > a->key) { b = p = a->rightChild; d = -1; else { b = p = a->leftChild; d = 1; while (p != y) { if (k > p->key) { p->bf = -1; p = p->rightChild; p->bf = 1; p = p->leftChild; 1 pa 8 d=-1 -1 a -1 2 9 b p 1 1 5 10 3 6 -1 p y 4 p

Rotation - LL if (a == NULL) return true; else if (a->bf==0 || a->bf+d == 0) { a->bf += d; } if (d == 1) //left imbalance { if (b->bf == 1) //rotation type LL a->leftChild = b->rightChild; b->rightChild = a; a->bf = 0; b->bf = 0; a b A B b a B C A TA C TB TB TA

Rotation - LR else //rotation type LR { c = b->rightChild; b->rightChild = c->leftChild; a->leftChild = c->rightChild; c->leftChild = b; c->rightChild = a; switch (c->bf) { case 1: a->bf = -1; b->bf = 0; break; case -1: b->bf = 1; a->bf = 0; break; case 0: b->bf = 0; b->bf = 0; break; } c->bf = 0; b = c; //b is the new root a A B C TA TCL TCR b c c C B A TA TCL TCR b a

else { //right imbalance. This is symmetric to left imbalance } if (pa == NULL) root = b; else if (a == pa->leftChild) pa->leftChild = b; else pa->rightChild = b; return true; 10 8 9 2 1 pa pa 10 2 9 1 8 a b b LL a