This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Other Balanced Trees, and an Unbalanced One (Huffman Encoding) EECS 2332 Previous Lecture ■ AVL trees (selfbalancing binary tree) ■ Rotation operations: left rotation, right rotation, double rotation3 Previous Lecture ■ Insertion may cause change of balance value ■ Insertion algorithm Insert a node, which may cause a change to balance values For each ancestor (from the leaf to the root), if the balance value ✔ changes from +/1 to 0, DONE ✔ changes from 0 to +/ 1, go to the next ancestor ✔ changes from +/1 to +/ 2, rebalance this tree, DONE How to balance the tree? If insertion occurred in the ✔ leftleft subtree, do right rotation ✔ rightright subtree, do left rotation ✔ leftright subtree, do leftright double rotation ✔ rightleft subtree, do rightleft double rotation4 Deletion from AVL Trees ■ Deletion may also cause unbalanced subtrees; if a deletion causes a change to the height, the ancestors need to be checked or balanced. ■ Deletion algorithm remove the node (leaf or nonleaf, remember how it works? ) Ultimately causes removal of a node X with 0 or 1 child May cause a change of the balance values for X’s parent For each ancestor (from X’s parent to the root), if the balance value ✔ changes from +/1 to 0, what to do? ✔ changes from 0 to +/ 1, what to do? ✔ changes from +/1 to +/ 2, what to do? To perform the balancing, use the same rotations as in insertion 5 Efficiency of AVL Trees ■ An AVL tree containing n items has a height that is O (log 2 n). ■ Search and insertion are both O (log 2 n). Search travels at most one path down the tree An insertion goes down one path to the insertion point, and then goes back up adjusting balances/performing rotations ✔ in the worst case, both the path down and the path back up...
View
Full
Document
 Spring '08
 Rabinovich

Click to edit the document details