1 joe meehean. bst efficiency relies on height lookup, insert, delete: o(height) a balanced tree...

44
AVL Trees 1 Joe Meehean

Upload: jane-georgina-bryant

Post on 31-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

CS242 Data Structures II

AVL Trees1Joe Meehean

ProblemBST efficiency relies on heightlookup, insert, delete: O(height)a balanced tree has the smallest heightWe can balance an unbalanced treeDSWexpensiveHow can we ensure that our BST stays balanced?after inserts and deletes

2AVL TreesAdelson-Velskii and Landis

Binary search tree

Additional balance conditionfor every nodeheight of subtrees differ by at most 1must store height of each nodeempty tree has height of -1

3InsertInsert as normal (BST)Update heightsCheck balance conditionheight differences of nodesalong path from new node to rootUse rotation to fix imbalances

4InsertFixing imbalance at node A4 cases: Insertion intoleft subtree of left child of Aright subtree of left child of Aleft subtree of right child of Aright subtree of right child of ACases 1 & 4 are outside insertsCases 2 & 3 are inside inserts

5Outside InsertInsert key less than A & B

6ABXYZh: 2h: 2h: 3h: 2h: 4Outside InsertInsert key less than A & B

7ABXYZh: 2h: 2h: 3h: 2h: 4ABXYZh: 2h: 3h: 4h: 2h: 5What series of rotations can we do to fix this?8DISCUSSIONBREAK!!!Outside InsertRotate A with highest subtree

9ABXYZh: 2h: 3h: 4h: 2h: 5Outside InsertRotate A with highest subtree

10ABYZABYZXXh: 2h: 3h: 4h: 2h: 5h: 2h: 3h: 4h: 3h: 2Outside InsertAre we done?Worry about imbalances above B?11ABYZXh: 2h: 3h: 4h: 3h: 2Outside InsertB tree same height asA tree before insertProperty of outside insert rebalances

Right-right imbalance similar

12ABYZXh: 2h: 3h: 4h: 3h: 2Outside InsertMore specificallyBalance factor = h(left) h(right)If node N has a balance factor of 2,left subtree (L) is too highif L has a balance factor of 1rotate N & LIf node N has a balance factor of -2,right subtree (R) is too highif R has a balance factor of -1rotate N & R

13Questions?14InsertFixing imbalance at node A4 cases: Insertion intoleft subtree of left child of Aright subtree of left child of Aleft subtree of right child of Aright subtree of right child of ACases 1 & 4 are outside insertsCases 2 & 3 are inside inserts

15Inside InsertInsert key less than A, greater than B

16ABXYZh: 2h: 2h: 3h: 2h: 4Inside InsertInsert key less than A, greater than B

17ABXYZh: 2h: 2h: 3h: 2h: 4ABXYZh: 3h: 2h: 4h: 2h: 5Inside InsertLets try to rotate A & B again

18ABXZh: 3h: 2h: 4h: 2h: 5YInside InsertLets try to rotate A & B again

19ABXZh: 3h: 2h: 4h: 2h: 5YABXZh: 3h: 2h: 4h: 2h: 5YInside InsertLets expand Y

20ABXZh: 3h: 2h: 4h: 2h: 5QRCABXZh: 3h: 2h: 4h: 2h: 5Yh: 1h: 2Inside InsertWhat if we made C the root

21ABXZh: 3h: 2h: 4h: 2h: 5RCh: 2ABXZh: 3h: 1h: 3h: 2h: 4Ch: 2h: 2QRh: 1QWhat series of rotations can we do to accomplish this?22DISCUSSIONBREAK!!!Inside InsertRotate left child (B) with its right child (C)

23ABXZh: 3h: 1h: 4h: 2h: 5Ch: 2h: 2ABXZh: 3h: 2h: 4h: 2h: 5Ch: 2QRh: 1QRInside InsertRotate root (A) with its new left child (C)

24ABXZh: 3h: 1h: 3h: 2h: 4Ch: 2h: 2ABXZh: 3h: 2h: 4h: 2h: 5QRCh: 2h: 2QRInside InsertAre we done?Worry about imbalances above ?25ABXZh: 3h: 1h: 3h: 2h: 4Ch: 2h: 2QRInside InsertC tree same height asA tree before insertProperty of inside insert rebalances

Right-left imbalance similar26ABXZh: 3h: 1h: 3h: 2h: 4Ch: 2h: 2QRInside InsertMore generallyIf there is an inside imbalance at AFind As highest child, BRotate B with its highest child, CRotate A with C

27Inside InsertMore specificallyBalance factor = h(left) h(right)If node N has a balance factor of 2,left subtree (L) is too highif L has a balance factor of -1rotate L with Ls right childrotate N with its new left child28Inside InsertMore specificallyBalance factor = h(left) h(right)If node N has a balance factor of -2,right subtree (R) is too highif R has a balance factor of 1rotate R with Rs left childrotate N with its new right child29Questions?30BST Delete ReviewFind the node N w/ key to be deletedDifferent actions depending on Ns # of kidsCase 1: N has 0 kids (its a leaf)set parents N-pointer (left or right) to nullCase 2: N has 1 kidset parents N-pointer to point to Ns only kidCase 3: N has 2 kidsReplace Ns key with a key further down in the treeDelete that node31BST Delete ReviewWhat node value can replace ns value?new value of n must be:> all values in left subtree< all values in right subtreeLargest value from the left subtreeSmallest value from the right subtreelets choose this one (arbitrarily)use findMin on root of right subtree32DeletionDeletes can also imbalance an AVL treeCan we fix these imbalances with rotations too?How many rotations do we need to do?33Deletion7555506057Smallest value in right subtreedelete(50)34XYZh: 2h: 3h: 1h: 0h: 2h: 465h: 1h: 562h: 0DeletionCopy smallestkey in R subtree357555556057delete(50)XYZh: 2h: 3h: 1h: 0h: 2h: 465h: 1h: 562h: 0Deletiondelete(55) fromR subtree3675575560delete(50)XYZh: 2h: 3h: 0h: 2h: 465h: 1h: 562h: 0Deletion75575560Fix imbalancedelete(50)37XYZh: 1h: 2h: 0h: 2h: 365h: 1h: 562h: 0DeletionNew imbalance

Need to rebalance all the way up to root3875575560delete(50)XYZh: 1h: 2h: 0h: 365h: 562h: 0DeletionMore generallyPerform a BST deleteCheck for imbalances along path to rootRebalance using expanded insert rulesif delete happened in light childheavy child may have a balance factor of 0additional case from insertion rules

39Rebalance RulesBalance factor = h(left) h(right)Given node N and its two children L & Rbf(N) == -2 & bf(R) == 0single right child rotationbf(N) == -2 & bf(R)== -1single right child rotationbf(N) == -2 & bf(R) == 1double right child rotation (from inside insert)40Rebalance RulesBalance factor = h(left) h(right)Given node N and its two children R & Lbf(N) == 2 & bf(L) == 0single left child rotationbf(N) == 2 & bf(L) == 1single left child rotationbf(N) == 2 & bf(L) == -1double left child rotation (from inside insert)41SummaryBSTinsert, delete, lookup O(H), H is height of treeAVLlike BST, but ensures balancebalanced tree has height of log NBalancingrotations on insert and delete

42SummaryAVL ComplexityInsertinserting node: O(H) == O(logN)rebalance: O(rotations) == O(2) == O(1)Erasedeleting a node: O(H) == O(logN)rebalance: O(rotations) == O(logN)

43Questions?44