440 likes | 639 Views
AVL Trees. Joe Meehean. Problem. BST efficiency relies on height lookup, insert, delete: O(height) a balanced tree has the smallest height We can balance an unbalanced tree DSW expensive How can we ensure that our BST stays balanced? after inserts and deletes. AVL Trees.
E N D
AVL Trees Joe Meehean
Problem • BST efficiency relies on height • lookup, insert, delete: O(height) • a balanced tree has the smallest height • We can balance an unbalanced tree • DSW • expensive • How can we ensure that our BST stays balanced? • after inserts and deletes
AVL Trees • Adelson-Velskii and Landis • Binary search tree • Additional balance condition • for every node • height of subtrees differ by at most 1 • must store height of each node • empty tree has height of -1
Insert • Insert as normal (BST) • Update heights • Check balance condition • height differences of nodes • along path from new node to root • Use rotation to fix imbalances
Insert • Fixing imbalance at node A • 4 cases: Insertion into • left subtree of left child of A • right subtree of left child of A • left subtree of right child of A • right subtree of right child of A • Cases 1 & 4 are outside inserts • Cases 2 & 3 are inside inserts
Outside Insert A h: 4 h: 3 h: 2 B Z h: 2 h: 2 X Y • Insert key less than A & B
Outside Insert A h: 5 A h: 4 h: 4 h: 2 h: 3 h: 2 B Z B Z h: 3 h: 2 h: 2 h: 2 X Y X Y • Insert key less than A & B
DISCUSSION BREAK!!! What series of rotations can we do to fix this?
Outside Insert A h: 5 h: 4 h: 2 B Z h: 3 h: 2 X Y • Rotate A with “highest” subtree
Outside Insert A B h: 5 h: 4 h: 4 h: 2 h: 3 A h: 3 B Z X h: 2 h: 3 h: 2 h: 2 X Z Y Y • Rotate A with “highest” subtree
Outside Insert B h: 4 h: 3 A h: 3 X h: 2 h: 2 Z Y Are we done? Worry about imbalances above B?
Outside Insert B h: 4 h: 3 A h: 3 X h: 2 h: 2 Z Y • B tree same height asA tree before insert • Property of outside insert rebalances • Right-right imbalance similar
Outside Insert • More specifically • Balance factor = h(left) – h(right) • If node N has a balance factor of 2, • left subtree (L) is too high • if L has a balance factor of 1 • rotate N & L • If node N has a balance factor of -2, • right subtree(R) is too high • if Rhas a balance factor of -1 • rotate N& R
Insert • Fixing imbalance at node A • 4 cases: Insertion into • left subtree of left child of A • right subtree of left child of A • left subtree of right child of A • right subtree of right child of A • Cases 1 & 4 are outside inserts • Cases 2 & 3 are inside inserts
Inside Insert A h: 4 h: 3 h: 2 B Z h: 2 h: 2 X Y • Insert key less than A, greater than B
Inside Insert A h: 4 A h: 5 h: 3 h: 2 h: 4 h: 2 B Z B Z h: 2 h: 2 h: 2 h: 3 X Y X Y • Insert key less than A, greater than B
Inside Insert A h: 5 h: 4 h: 2 B Z h: 2 h: 3 X Y • Let’s try to rotate A & B again
Inside Insert A h: 5 B h: 5 h: 4 h: 2 h: 4 h: 2 X A B Z h: 2 h: 3 h: 3 h: 2 X Y Y Z • Let’s try to rotate A & B again
Inside Insert A h: 5 A h: 5 h: 4 h: 2 B Z h: 4 h: 2 B Z h: 3 h: 2 C X h: 2 h: 3 X Y h: 2 R Q h: 1 • Let’s expand Y
Inside Insert A h: 5 h: 4 C h: 3 h: 3 h: 4 h: 2 A B B Z h: 3 h: 2 h: 2 Z R h: 2 C X h: 2 h: 1 X h: 2 R h: 1 Q Q • What if we made C the root
DISCUSSION BREAK!!! What series of rotations can we do to accomplish this?
Inside Insert A A h: 5 h: 5 h: 4 h: 2 h: 4 h: 2 C B Z Z h: 3 h: 3 B R h: 2 h: 2 C X R h: 2 X h: 2 h: 1 Q Q h: 1 • Rotate left child (B) with its right child (C)
Inside Insert h: 4 A h: 5 C h: 3 h: 3 h: 4 h: 2 A C B Z h: 3 B h: 2 h: 2 R R Z h: 2 h: 2 h: 1 X Q X h: 2 h: 2 Q • Rotate root (A) with its new left child (C)
Inside Insert h: 4 C h: 3 h: 3 A B h: 2 h: 2 R Z h: 2 h: 1 X Q Are we done? Worry about imbalances above ?
Inside Insert h: 4 C h: 3 h: 3 A B h: 2 h: 2 R Z h: 2 h: 1 X Q C tree same height asA tree before insert Property of inside insert rebalances Right-left imbalance similar
Inside Insert More generally If there is an inside imbalance at A Find A’s highest child, B Rotate B with its highest child, C Rotate A with C
Inside Insert • More specifically • Balance factor = h(left) – h(right) • If node N has a balance factor of 2, • left subtree (L) is too high • if L has a balance factor of -1 • rotate L with L’s right child • rotate N with its new left child
Inside Insert • More specifically • Balance factor = h(left) – h(right) • If node N has a balance factor of -2, • right subtree (R) is too high • if R has a balance factor of 1 • rotate R with R’s left child • rotate N with it’s new right child
BST Delete Review • Find the node N w/ key to be deleted • Different actions depending on N’s # of kids • Case 1: N has 0 kids (it’s a leaf) • set parent’s N-pointer (left or right) to null • Case 2: N has 1 kid • set parent’s N-pointer to point to N’s only kid • Case 3: N has 2 kids • Replace N’s key with a key further down in the tree • Delete that node
BST Delete Review • What node value can replace n’s value? • new value of n must be: • > all values in left subtree • < all values in right subtree • Largest value from the left subtree • Smallest value from the right subtree • let’s choose this one (arbitrarily) • use findMin on root of right subtree
Deletion Deletes can also imbalance an AVL tree Can we fix these imbalances with rotations too? How many rotations do we need to do?
Deletion 50 delete(50) h: 4 h: 5 X 75 h: 2 h: 3 Y 60 Smallest value in right subtree h: 1 55 h: 2 65 h: 1 h: 0 h: 0 Z 57 62
Deletion 55 delete(50) h: 4 h: 5 X 75 h: 2 h: 3 Y 60 Copy smallest key in R subtree h: 1 55 h: 2 65 h: 1 h: 0 h: 0 Z 57 62
Deletion 55 delete(50) h: 4 h: 5 X 75 h: 2 h: 3 Y 60 delete(55) from R subtree 57 h: 2 65 h: 0 h: 1 h: 0 Z 62
Deletion 55 delete(50) h: 3 h: 5 X 75 h: 2 h: 2 Y 65 Fix imbalance h: 1 h: 1 60 Z h: 0 h: 0 62 57
Deletion 55 delete(50) h: 3 h: 5 X 75 h: 2 Y 65 h: 1 60 Z New imbalance Need to rebalance all the way up to root h: 0 h: 0 62 57
Deletion • More generally • Perform a BST delete • Check for imbalances along path to root • Rebalance using expanded insert rules • if delete happened in “light” child • “heavy” child may have a balance factor of 0 • additional case from insertion rules
Rebalance Rules • Balance factor = h(left) – h(right) • Given node N and its two children L& R • bf(N) == -2 & bf(R) == 0 • single right child rotation • bf(N) == -2 & bf(R)== -1 • single right child rotation • bf(N) == -2 & bf(R) == 1 • double right child rotation (from inside insert)
Rebalance Rules • Balance factor = h(left) – h(right) • Given node N and its two children R & L • bf(N) == 2 & bf(L) == 0 • single left child rotation • bf(N) == 2 & bf(L) == 1 • single left child rotation • bf(N) == 2 & bf(L) == -1 • double left child rotation (from inside insert)
Summary • BST • insert, delete, lookup O(H), H is height of tree • AVL • like BST, but ensures balance • balanced tree has height of log N • Balancing • rotations on insert and delete
Summary • AVL Complexity • Insert • inserting node: O(H) == O(logN) • rebalance: O(rotations) == O(2) == O(1) • Erase • deleting a node: O(H) == O(logN) • rebalance: O(rotations) == O(logN)