AVL inser0on postcondi0on When you insert into an AVL tree of height - - PDF document

avl inser0on postcondi0on
SMART_READER_LITE
LIVE PREVIEW

AVL inser0on postcondi0on When you insert into an AVL tree of height - - PDF document

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 hasnt had any rota+ons performed on


slide-1
SLIDE 1

10/29/15 1

AVL inser0on postcondi0on

When you insert into an AVL tree of height h, either

  • you get a new tree with the same

height (which may have had rota0ons performed), or

  • you get a tree that hasn’t had any

rota+ons performed on it, with an 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.

h h h h+1 h h-1 h-2 h h-2 h-1

How could a viola0on of the AVL height invariant happen at node T?

inser0on into a leK subtree could lead to a viola0on h+1 h h-2 h+1 h-2 h inser0on into a right subtree could lead to a viola0on (the red nodes violate the AVL height invariant)

T T T T

slide-2
SLIDE 2

10/29/15 2

h h-1 h-2

What does that leK subtree look like before the inser0on?

(right subtree discussion is similar – not shown in remaining slides)

h-1 h-2 h-3 h-2 h-2 h-1 h-1 h-3 h-2

T

h h-1 h-2 h-2 h-3 h+1 h h-2 h-1 h-3 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.)

T T

slide-3
SLIDE 3

10/29/15 3

h-1 h-2 h-3 h-2 h h h-2 h-3 h-1 h+1 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 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

  • nly node T, so this is the only situa0on that we need to consider.

T

slide-4
SLIDE 4

10/29/15 4

h h-1 h-2 h-2 h-2 case 1: inser0on into subtree A case 2: inser0on into subtree B h+1 C h h-2 h-1 B h-2 A h+1 C h h-2 h-2 A h-1 B

T

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

  • r the right subtree of T's leK child.

T T

h+1 h h-2 h-1 h-2 A B C x y h h-1 h-2 h-1 h-2 A B C x y rotate right

Case 1: Single rota0on fixes the invariant

T

In the first case, a single rota0on right fixes the tree at node T. newT = T->leK T->leK = T->leK->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.)

T

rotate right

slide-5
SLIDE 5

10/29/15 5

h C h-1 h-2 A h-2 B h-2 inser0on into middle subtree h+1 C h h-2 h-2 A h-1 B

T T

In the second case, we need to examine the structure of subtree B.

Case 2 is more involved

h h-1 h-2 h-2 h-3 h-3 h-2 inser0on into mid-leK subtree inser0on into mid-right subtree h+1 h h-2 h-1 h-2 h-3 h-2 h+1 h h-2 h-1 h-3 h-2 h-2 Again, these two subtrees must be the same height, otherwise we would have a viola0on lower in the tree as well as at T.

T T T

A A A B1 B1 B1 B2 B2 B2 C C C

slide-6
SLIDE 6

10/29/15 6

h+1 h h-2 h-1 h-2 x y z A B1 B2 C h+1 h h-2 h-2 x y z A B1 B2 C h-1 h-1 h h-2 h-2 x y z A B1 B2 C h-1

Case 2: Double rota0on fixes the tree

h-3 and h-2

  • r

h-2 and h-3 h

  • 3

a n d h

  • 2
  • r

h

  • 2

a n d h

  • 3

h-3 and h-2

  • r

h-2 and h-3 rotate le: rotate right 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.)

T T T

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.