10/29/15 AVL inser0on postcondi0on When you insert into an AVL tree of height h , either you get a new tree with the same • h h height (which may have had rota0ons performed), or you get a tree that hasn’t had any • rota+ons performed on it , with an h h+1 increased height of at most one When an insert is done in an AVL tree, nodes are checked one by one, moving up toward the root to make sure the height invariant con0nues to hold. How could a viola0on of the AVL height invariant happen at node T? T T h+1 h inser0on into h h-2 h-1 h-2 a leK subtree could lead to a viola0on T T h h+1 inser0on into h-2 h-1 h-2 h a right subtree could lead to a viola0on (the red nodes violate the AVL height invariant) 1
10/29/15 What does that leK subtree look like before the inser0on? (right subtree discussion is similar T – not shown in remaining slides) h h-1 h-2 h-1 h-1 h-1 h-2 h-2 h-2 h-3 h-2 h-3 T T h h+1 h-1 h-2 h h-2 h-3 h-2 h-3 h-1 In the first situa0on, the AVL height invariant holds for node T. If we insert into the tree, we could violate the invariant not just at the node T but at its leK child. This shouldn't happen since the height invariant should already hold for the children of the node T from a previous check. (The tree is checked boQom to top aKer an inser0on is done.) 2
10/29/15 T T h h+1 h h-2 h-1 h-2 h-2 h-1 h-3 h-3 In the second situa0on, the AVL height invariant holds for the node T. If we insert into the tree, we could violate the invariant not just at node T but at its leK child. This shouldn't happen since the height invariant should already hold for the children of the node T from a previous check. (The tree is checked boQom to top aKer an inser0on is done.) T h h-1 h-2 h-2 h-2 In the third situa0on, the AVL height invariant holds for the node T. If we insert into the leK subtree of T, we could violate the invariant at only node T, so this is the only situa0on that we need to consider. 3
10/29/15 T h+1 h h-2 case 1: inser0on C T h-2 h-1 into subtree A h B A h-1 h-2 h-2 h-2 T h+1 case 2: inser0on h h-2 into subtree B C h-1 h-2 A B To violate the height invariant for node T, the inserted value must either increase the height of the leK subtree of T's leK child or the right subtree of T's leK child. Case 1: Single rota0on fixes the invariant T T h h+1 y x h h-2 h-1 x h-1 y C rotate right h-2 h-1 h-2 h-2 A C B B A In the first case, a single rota0on right fixes the tree at node T. newT = T->leK T->leK = T->leK->right rotate right newT->right = T T = newT (Note that the heights of the subtrees remains the same, but the heights of the nodes labeled x and y change.) 4
10/29/15 Case 2 is more involved T T h+1 h h-2 h h-1 h-2 inser0on into middle subtree C C h-1 h-2 h-2 h-2 A A B B In the second case, we need to examine the structure of subtree B. T h+1 h h-2 C h-2 h-1 inser0on into T mid-leK subtree A h-3 h h-2 B 2 B 1 h-1 h-2 C h-2 h-2 T A h+1 h-3 h-3 inser0on into B 1 B 2 mid-right subtree h h-2 Again, these two C subtrees must be the h-2 h-1 same height, otherwise A we would have a viola0on h-2 h-3 lower in the tree as well as B 1 at T. B 2 5
10/29/15 Case 2: Double rota0on fixes the tree T T h+1 h+1 T rotate le: rotate right z z h h h y y x C C h-1 h-1 h-1 h-1 h-2 h-2 x x z y B 2 A 2 - h-2 B 1 h B 1 B 2 d n C B 1 B 2 A a A 3 - h h-3 and h-2 3 h-2 - h-2 h-2 h h-3 and h-2 r d o n or a 2 or - h h-2 and h-3 h-2 and h-3 Perform rotate leK on T->leK, which reverses the roles of x and y. Then perform rotate right on T, which reverses the roles of y and z, balancing the tree at node T. (Note that in the middle of this double rota0on, two nodes violate the height invariant temporarily.) More to consider How would you write the rotate le: opera0on? How would you analyze the case where an inser0on into the right subtree of T causes a height viola0on? Show that in any of these rota0ons, the ordering invariant con0nues to hold. 6
Recommend
More recommend