Lecture11 - Other Balanced Trees and an Unbalanced...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Other Balanced Trees, and an Unbalanced One (Huffman Encoding) EECS 233-2- Previous Lecture ■ AVL trees (self-balancing binary tree) ■ Rotation operations: left rotation, right rotation, double rotation-3- 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 ✔ left-left subtree, do right rotation ✔ right-right subtree, do left rotation ✔ left-right subtree, do left-right double rotation ✔ right-left subtree, do right-left double rotation-4- 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 non-leaf, 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

Page1 / 23

Lecture11 - Other Balanced Trees and an Unbalanced...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online