1 / 12

AVL Trees

AVL Trees. CS 110: Data Structures and Algorithms First Semester, 2010-2011. Overview. BSTs can become skewed. Inserting elements in ascending or descending order creates a skewed BST. Certain deletions also skew the tree. Self-balancing Trees

Download Presentation

AVL Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AVL Trees CS 110: Data Structures and Algorithms First Semester, 2010-2011

  2. Overview • BSTs can become skewed. • Inserting elements in ascending or descending order creates a skewed BST. • Certain deletions also skew the tree. • Self-balancing Trees • Prevents skewed trees by modifying the tree after operations • AVL Tree • Self-balances by rotating subtrees after insertions and deletions

  3. (Sub)Tree Rotation C A B B B 0 3 A C A 1 C 2 0 0 1 2 1 3 2 3 Rotate Right Rotate Right Rotate Left Rotate Left

  4. Rotation Code public static Node rotateRight(final Node root) { final Node pivot = root.leftChild; if ( pivot == null ) return root; root.leftChild = pivot.rightChild; pivot.rightChild = root; return pivot; } public static Node rotateLeft(final Node root) { final Node pivot = root.rightChild; if ( pivot == null ) return root; root.rightChild = pivot.leftChild; pivot.leftChild = root; return pivot; }

  5. Rotation Code (w/reparenting) public static Node rotateRight(final Node root){ final Node pivot = root.leftChild; if ( pivot == null ) return root; root.leftChild = pivot.rightChild; pivot.rightChild.parent = root; pivot.rightChild = root; if ( root.parent != null ) { if ( root.parent.leftChild == root ) root.parent.leftChild = pivot; else root.parent.rightChild = pivot; } pivot.parent = root.parent; root.parent = pivot; return pivot; }

  6. Balance Factor • The balance factor of the node n is: Bal(n) = height(n.leftChild) – height(n.rightChild) • An AVL tree guarantees that |Bal(n)| < 2 for any node n. • If |Bal(n)| ≥ 2 then the node n is said to be unbalanced. • Tree rotations are performed to rebalance. • Rebalancing is performed after insertion and deletion.

  7. AVL Insertion • Insertion is done the usual way. • After insertion, we need to check the balance factor of every ancestor of the inserted node and rebalance if necessary. • There are 4 cases to consider if a node is unbalanced, 2 of which are symmetric to the other two.

  8. Rebalancing After Insertion • If Bal(n) = -2 (i.e. the right is “heavier” than the left) • Case 1: if Bal(n.rightChild) = -1 • rotateLeft(n) • Case 2: if Bal(n.rightChild) = +1 • rotateRight(n.rightChild) then rotateLeft(n) A A B C 0 0 Case 1: Case 2: C 3 B 1 3 2 2 1

  9. Rebalancing After Insertion • If Bal(n) = +2 (i.e. the left is “heavier” than the right) • Case 3: if Bal(n.leftChild) = +1 • rotateRight(n) • Case 4: if Bal(n.leftChild) = -1 • rotateLeft(n.leftChild) then rotateRight(n) C C B A 3 3 Case 4: Case 3: A 0 B 2 0 1 1 2

  10. AVL Deletion • If the node is a leaf, just remove it. • If the node has only one child, replace it with that child. • Else, replace it with its inorder predecessor (go to the left then keep on going right) or its inorder successor (go the right then keep on going left) then iterate the replacement process.

  11. Rebalancing After Deletion • Similar to insertion, the deleted node’s ancestors must be checked for rebalancing. • Start checking from the parent of the last deleted node. • If |Bal(n)| > 1 but |Bal(n.leftChild)| or |Bal(n.rightChild)| is zero. Do a single rotation on n.

  12. Missing Details • How to implement swapping on an array-based binary tree implementation. • How to determine the height of the node in O(lg n) time.

More Related