Presentation is loading. Please wait.

Presentation is loading. Please wait.

AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.

Similar presentations


Presentation on theme: "AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We."— Presentation transcript:

1 AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We want a tree with small height  Height of a binary tree with N node is at least O(log N)  Complete binary tree has height log N.  Goal: keep the height of a binary search tree O(log N)  Balanced binary search trees  Examples: AVL tree, red-black tree

2 AVL Trees / Slide 2 AVL tree for each node, the height of the left and right subtrees can differ by at most d = 1. Maintaining a stricter condition (e.g above condition with d = 0) is difficult. Note that our goal is to perform all the operations search, insert and delete in O(log N) time, including the operations involved in adjusting the tree to maintain the above balance condition.

3 AVL Trees / Slide 3 AVL Tree  An AVL tree is a binary search tree in which  for every node in the tree, the height of the left and right subtrees differ by at most 1.  Height of subtree: Max # of edges to a leaf  Height of an empty subtree: -1  Height of one node: 0 AVL tree property violated here AVL tree

4 AVL Trees / Slide 4 AVL Tree with Minimum Number of Nodes N 1 = 2 N 2 =4N 3 = N 1 +N 2 +1=7 N 0 = 1

5 AVL Trees / Slide 5 Smallest AVL tree of height 9 Smallest AVL tree of height 7 Smallest AVL tree of height 8

6 AVL Trees / Slide 6 Height of AVL Tree with N nodes  Denote N h the minimum number of nodes in an AVL tree of height h  N 0 = 1, N 1 =2 (base) N h = N h-1 + N h-2 +1 (recursive relation)  N > N h = N h-1 + N h-2 +1 > 2 N h-2 > 4 N h-4 >... >2 i N h-2i

7 AVL Trees / Slide 7 Height of AVL Tree with N nodes  If h is even, let i=h/2–1. The equation becomes N>2 h/2-1 N 2  N > 2 h/2-1 x4  h=O(log N)  If h is odd, let i=(h-1)/2. The equation becomes N>2 (h-1)/2 N 1  N > 2 (h-1)/2 x2  h=O(log N)  Recall the cost of operations search, insert and delete is O(h).  cost of searching = O(log N). (Just use the search algorithm for the binary search tree.)  But insert and delete are more complicated.

8 AVL Trees / Slide 8 Insertion in AVL Tree  Basically follows insertion strategy of binary search tree  But may cause violation of AVL tree property  Restore the destroyed balance condition if needed 6 7 68 Original AVL tree Insert 6 Property violated Restore AVL property

9 AVL Trees / Slide 9 Some Observations  After an insertion (using the insertion algorithm for a binary search tree), only nodes that are on the path from the insertion point to the root might have their balance altered.  Because only those nodes have their subtrees altered  So it’s enough to adjust the balance on these nodes.  We will see that AVL tree property can be restored by performing a balancing operation at a single node.

10 AVL Trees / Slide 10 Node at which balancing must be done  The node of smallest depth on the path from to the newly inserted node.  We will call this node pivot. Example: Suppose we inserted key is 2.5 Pivot is the node containing 4. This is the lowest depth node in the path where the AVL tree property is violated. 2.5 *

11 AVL Trees / Slide 11 Different Cases for Rebalance  Let α be the pivot node  Case 1: inserted node is in the left subtree of the left child of α (LL-rotation)  Case 2: inserted node is in the right subtree of the left child of α (RL-rotation)  Case 3: inserted node is in the left subtree of the right child of α (LR-rotation)  Case 4: inserted node is in the right subtree of the right child of α (RR-rotation)  Cases 3 & 4 are mirror images of cases 1 & 2 so we will focus on 1 & 2.

12 AVL Trees / Slide 12 Rotations  Rebalance of AVL tree are done with simple modification to tree, known as rotation  Insertion occurs on the “outside” (i.e., left-left or right-right) is fixed by single rotation of the tree  Insertion occurs on the “inside” (i.e., left-right or right-left) is fixed by double rotation of the tree

13 AVL Trees / Slide 13 Insertion Algorithm (outline)  First, insert the new key as a new leaf just as in ordinary binary search tree  Then trace the path from the new leaf towards the root. For each node x encountered, check if heights of left(x) and right(x) differ by at most 1.  If yes, proceed to parent(x)  If not, restructure by doing either a single rotation or a double rotation  Note: once we perform a rotation at a node x, we won’t need to perform any rotation at any ancestor of x.

14 AVL Trees / Slide 14 Single Rotation to Fix Case 1(LL-rotation) Questions:  Can Y have the same height as the new X?  Can Y have the same height as Z? k2 is the pivot node

15 AVL Trees / Slide 15 Single Rotation Case 1 Example k2 k1 X k2 X

16 AVL Trees / Slide 16 Informal proof/argument for the LL-case We need to show the following:  After the rotation, the balance condition at the pivot node is restored and at all the nodes in the subtree rooted at the pivot.  After the rotation, the balance condition is restored at all the ancestors of the pivot.  We will prove both these informally.

17 AVL Trees / Slide 17 Single Rotation to Fix Case 4 (RR-rotation)  Case 4 is a symmetric case to case 1  Insertion takes O(h) time (h = height of the tree), single rotation takes O(1) time. (including locating the pivot node). Details are not obvious, but see the code … An insertion in subtree Z k1 violates AVL tree balance condition

18 AVL Trees / Slide 18 Single Rotation Fails to fix Case 2&3  Single rotation fails to fix case 2&3  Take case 2 as an example (case 3 is a symmetry to it )  The problem is subtree Y is too deep  Single rotation doesn’t make it any less deep Single rotation result Case 2: violation in k2 because of insertion in subtree Y

19 AVL Trees / Slide 19 Double rotation This applies when the path taken from the pivot node to the inserted (leaf) node starts with RL or LR. Suppose n is the pivot node and its child on the path (to the inserted node) is c. Double rotation involves: Step 1: perform a single rotation about c. Step 2: perform a single rotation about n. Figure 4.39 shows the RL rotation. Figure 4.45 shows the LR rotation.

20 AVL Trees / Slide 20 Double rotation In lecture, we will provide an informal proof that this rotation restores the AVL tree and that no further adjustment is needed above the pivot node. This involves showing the following: 1. The Rotations preserves the binary tree property. (This is obvious since we showed earlier that single rotations preserve the binary tree property.) 2. The resulting tree is an AVL tree. 3. The height of the subtree about which the double rotation is performed is the same as the subtree (before the insertion) rooted at the pivot.

21 AVL Trees / Slide 21 Implementing AVL tree insertion Code shown below is taken from the text. It uses the (same) binary search tree class – with one additional data member, the depth. (Each node stores the depth of the subtree rooted at that node.) Example: 3 2 4 1 2 1 1

22 AVL Trees / Slide 22 Implementing AVL tree insertion We will also add a member function height that takes a tree pointer and returns the height field stored at the node. int height(AvlNode *t) { if (t == NULL) return 0; else return t->height; } We will now present the code for insertion.

23 AVL Trees / Slide 23 Note: the height information should be updated when insertion and deletion is done. void insert( const Comparable & x, AvlNode * & t ) { if( t == NULL ) t = new AvlNode( x, NULL, NULL ); else if( x element ) { insert( x, t->left ); if( height( t->left ) - height( t->right ) == 2 ) if( x left->element ) rotateWithLeftChild( t ); else doubleWithLeftChild( t ); }

24 AVL Trees / Slide 24 else if( t->element < x ) { insert( x, t->right ); if( height( t->right ) - height( t->left ) == 2 ) if( t->right->element < x ) rotateWithRightChild( t ); else doubleWithRightChild( t ); } else ; // Duplicate; do nothing t->height = max( height( t->left ), height( t->right ) ) + 1; }

25 AVL Trees / Slide 25 Code for left (single) rotation /* Rotate binary tree node with left child. * For AVL trees, this is a single rotation for case 1. * Update heights, then set new root. */ void rotateWithLeftChild( AvlNode * & k2 ) { AvlNode *k1 = k2->left; k2->left = k1->right; k1->right = k2; k2->height = max( height( k2->left ), height( k2->right ) ) + 1; k1->height = max( height( k1->left ), k2->height ) + 1; k2 = k1; }

26 AVL Trees / Slide 26 Code for double rotation void doubleWithLeftChild( AvlNode * & k3 ) { rotateWithRightChild( k3->left ); rotateWithLeftChild( k3 ); }


Download ppt "AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We."

Similar presentations


Ads by Google