AVL inser*on postcondi*on When you insert into an AVL - - PowerPoint PPT Presentation

avl inser on postcondi on
SMART_READER_LITE
LIVE PREVIEW

AVL inser*on postcondi*on When you insert into an AVL - - PowerPoint PPT Presentation

AVL inser*on postcondi*on When you insert into an AVL tree, either h h you get a new tree with the same height (which may have had


slide-1
SLIDE 1

AVL ¡inser*on ¡postcondi*on ¡

When ¡you ¡insert ¡into ¡an ¡ AVL ¡tree, ¡either ¡

  • you ¡get ¡a ¡new ¡tree ¡with ¡

the ¡same ¡height ¡(which ¡ may ¡have ¡had ¡rota*ons ¡ performed), ¡or ¡

  • you ¡get ¡a ¡tree ¡that ¡

hasn’t ¡had ¡any ¡rota+ons ¡ performed ¡on ¡it, ¡with ¡an ¡ increased ¡height ¡of ¡at ¡ most ¡one ¡

h ¡ h ¡ h ¡ h ¡ h ¡ h+1 ¡

slide-2
SLIDE 2

h ¡ h-­‑1 ¡ h-­‑2 ¡ h ¡ h-­‑2 ¡ h-­‑1 ¡

how ¡could ¡a ¡viola*on ¡happen? ¡

inser*on ¡into ¡ ¡ le@ ¡subtree ¡ h+1 ¡ h ¡ h-­‑2 ¡ h+1 ¡ h-­‑2 ¡ h ¡ inser*on ¡into ¡ ¡ right ¡subtree ¡ The ¡red ¡nodes ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡violate ¡the ¡AVL ¡height ¡invariant. ¡ Two ¡func*ons, ¡rebalance_le@ ¡and ¡rebalance_right, ¡will ¡handle ¡these ¡two ¡cases. ¡

slide-3
SLIDE 3

h ¡ h-­‑1 ¡ h-­‑2 ¡

what ¡does ¡that ¡le@ ¡subtree ¡look ¡like? ¡

inser*on ¡into ¡ ¡ le@ ¡subtree ¡ h+1 ¡ h ¡ h-­‑2 ¡ We’ll ¡just ¡consider ¡rebalance_le@ ¡for ¡now. ¡ ¡ To ¡figure ¡out ¡how ¡to ¡handle ¡the ¡viola*on, ¡we ¡must ¡look ¡at ¡how ¡the ¡le@ ¡subtree ¡is ¡constructed. ¡

slide-4
SLIDE 4

h ¡ h-­‑1 ¡ h-­‑2 ¡ h-­‑2 ¡ h-­‑2 ¡ h ¡ h-­‑1 ¡ h-­‑2 ¡ h-­‑2 ¡ h-­‑3 ¡ h-­‑1 ¡ h-­‑2 ¡ h-­‑3 ¡ h-­‑2 ¡ h ¡ We ¡know ¡the ¡AVL ¡tree ¡invariant ¡held ¡before, ¡so ¡the ¡le@ ¡subtree ¡can ¡only ¡take ¡one ¡of ¡three ¡forms. ¡ But ¡that’s ¡not ¡all ¡we ¡know! ¡ ¡

slide-5
SLIDE 5

h ¡ h-­‑1 ¡ h-­‑2 ¡ h-­‑2 ¡ h-­‑2 ¡ h ¡ h-­‑1 ¡ h-­‑2 ¡ h-­‑2 ¡ h-­‑3 ¡ h-­‑1 ¡ h-­‑2 ¡ h-­‑3 ¡ h-­‑2 ¡ h ¡ h+1 ¡ h ¡ h-­‑2 ¡ h-­‑1 ¡ h-­‑3 ¡ h ¡ h-­‑2 ¡ h-­‑3 ¡ h-­‑1 ¡ h+1 ¡ The ¡first ¡and ¡the ¡last ¡of ¡these ¡three ¡trees ¡are ¡impossible ¡ according ¡to ¡the ¡original ¡invariant, ¡because ¡they ¡cause ¡a ¡ viola*on ¡lower ¡in ¡the ¡tree! ¡Or, ¡opera*onally, ¡we ¡can ¡say ¡that ¡ if ¡the ¡tree ¡had ¡looked ¡like ¡the ¡first ¡or ¡last ¡of ¡these ¡three, ¡we ¡ would ¡have ¡already ¡no*ced, ¡and ¡fixed, ¡the ¡viola*on. ¡

slide-6
SLIDE 6

h ¡ h-­‑1 ¡ h-­‑2 ¡ h-­‑2 ¡ h-­‑2 ¡ inser*on ¡into ¡ ¡ far ¡le@ ¡subtree ¡ inser*on ¡into ¡ middle ¡subtree ¡ h+1 ¡ h ¡ h-­‑2 ¡ h-­‑1 ¡ h-­‑2 ¡ h+1 ¡ h ¡ h-­‑2 ¡ h-­‑2 ¡ h-­‑1 ¡ So ¡we’re ¡le@ ¡with ¡this ¡picture ¡for ¡rebalance_le@: ¡ ¡ two ¡possible ¡ways ¡that ¡BST ¡inser*on ¡could ¡have ¡violated ¡the ¡AVL ¡height ¡invariant ¡at ¡the ¡root. ¡

slide-7
SLIDE 7

h ¡ h-­‑1 ¡ h-­‑2 ¡ h-­‑2 ¡ h-­‑2 ¡ inser*on ¡into ¡ ¡ far ¡le@ ¡subtree ¡ h+1 ¡ h ¡ h-­‑2 ¡ h-­‑1 ¡ h-­‑2 ¡ ¡ We ¡can ¡immediately ¡see ¡how ¡to ¡resolve ¡the ¡first ¡case… ¡

slide-8
SLIDE 8

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 ¡ rebalance ¡

fixup: ¡single ¡rota*on ¡

¡ …a ¡single ¡right ¡rota*on ¡at ¡the ¡root. ¡

slide-9
SLIDE 9

h ¡ h-­‑1 ¡ h-­‑2 ¡ h-­‑2 ¡ h-­‑2 ¡ inser*on ¡into ¡ ¡ far ¡le@ ¡subtree ¡ inser*on ¡into ¡ middle ¡subtree ¡ h+1 ¡ h ¡ h-­‑2 ¡ h-­‑1 ¡ h-­‑2 ¡ h+1 ¡ h ¡ h-­‑2 ¡ h-­‑2 ¡ h-­‑1 ¡ ¡ That ¡takes ¡care ¡of ¡the ¡first ¡case… ¡

slide-10
SLIDE 10

h ¡ h-­‑1 ¡ h-­‑2 ¡ h-­‑2 ¡ h-­‑2 ¡ inser*on ¡into ¡ middle ¡subtree ¡ h+1 ¡ h ¡ h-­‑2 ¡ h-­‑2 ¡ h-­‑1 ¡ …now ¡let’s ¡look ¡at ¡the ¡second ¡case. ¡ To ¡see ¡how ¡this ¡works, ¡we’ll ¡need ¡to ¡examine ¡the ¡structure ¡of ¡the ¡middle ¡tree. ¡

slide-11
SLIDE 11

h ¡ h-­‑1 ¡ h-­‑2 ¡ h-­‑2 ¡ h-­‑3 ¡ h-­‑4 ¡ h-­‑2 ¡ h+1 ¡ h ¡ h-­‑2 ¡ h-­‑1 ¡ h-­‑2 ¡ h-­‑4 ¡ h-­‑2 ¡ By ¡the ¡same ¡reasoning ¡as ¡before, ¡we ¡know ¡that ¡the ¡middle ¡subtree ¡can’t ¡itself ¡have ¡ subtrees ¡with ¡different ¡heights, ¡or ¡there ¡would ¡be ¡a ¡lower ¡viola*on ¡of ¡the ¡height ¡invariant. ¡

slide-12
SLIDE 12

h ¡ h-­‑1 ¡ h-­‑2 ¡ h-­‑2 ¡ h-­‑3 ¡ h-­‑3 ¡ h-­‑2 ¡ inser*on ¡into ¡ ¡ mid-­‑le@ ¡subtree ¡ inser*on ¡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 ¡ So ¡both ¡middle ¡subtrees ¡have ¡height ¡h-­‑3, ¡and ¡one ¡of ¡them ¡has ¡height ¡h-­‑2 ¡a@er ¡the ¡inser*on. ¡ Again ¡we ¡have ¡two ¡cases! ¡Luckily, ¡we ¡can ¡deal ¡with ¡both ¡the ¡same ¡way. ¡

slide-13
SLIDE 13

h+1 ¡ h ¡ h-­‑2 ¡ h-­‑1 ¡ h-­‑3 ¡ ¡and ¡h-­‑2 ¡

  • r ¡

h-­‑2 ¡and ¡h-­‑3 ¡ h-­‑2 ¡ x ¡ y ¡ z ¡ A ¡ B ¡ C ¡ D ¡ h+1 ¡ h ¡ h-­‑2 ¡ h-­‑2 ¡ x ¡ y ¡ z ¡ A ¡ B ¡ C ¡ D ¡ h-­‑1 ¡ h-­‑1 ¡ h ¡ h-­‑2 ¡ h-­‑2 ¡ x ¡ y ¡ z ¡ A ¡ B ¡ C ¡ D ¡ h-­‑1 ¡

fixup: ¡double ¡rota*on ¡

h-­‑3 ¡ ¡and ¡h-­‑2 ¡

  • r ¡

h-­‑2 ¡and ¡h-­‑3 ¡ The ¡solu*on ¡is ¡the ¡same ¡regardless ¡of ¡whether ¡the ¡inser*on ¡went ¡into ¡B ¡or ¡C. ¡ We ¡rotate ¡twice: ¡first, ¡le@, ¡at ¡T-­‑>le@, ¡and ¡then, ¡right, ¡at ¡T. ¡