1 / 20

CS 5243: Algorithms

CS 5243: Algorithms. Balanced Trees AVL : Adelson-Velskii and Landis(1962). AVL Trees. An AVL tree is a binary tree that is height-balanced where the difference in height between the left and right subtrees at any point in the tree is restricted.

Download Presentation

CS 5243: Algorithms

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. CS 5243: Algorithms Balanced Trees AVL : Adelson-Velskii and Landis(1962)

  2. AVL Trees An AVL tree is a binary tree that is height-balanced where the difference in height between the left and right subtrees at any point in the tree is restricted. Define the balance factor of node x to be the height of x’s left subtree minus the height of it right subtree An AVL tree is a BST in which the balance factor of each node is 0, -1, or 1

  3. Sample Trees -1 1 0 1 0 1 0 0 0 0 0 1 -1 0 0 2 2 -2 0 1 Not AVL trees 1 0 0

  4. Keeping AVL trees balanced • When a new item inserted into a balanced binary tree • resulting tree may become unbalanced • Tree can be rebalanced • transform subtree rooted at the node that is the nearest ancestor of the new node unacceptable balance factor • This transformation carried out by rotation, the relative order of nodes in a subtree is shifted, changing the root, and the number of nodes in the left and right subtrees • Four standard types of rotations are performed on AVL trees:

  5. AVL Inserting • Do a normal BST insert • If the tree is balanced, you are finished. Otherwise do 3-4 • Starting at the newly inserted node, work your way up the tree until you find the first node whose subtrees’ heights differ by 2. This node is the pivot, the root of its taller subtree is the rotator. • It is necessarily the case that the new value was inserted in one of the rotator’s subtrees. If it was inserted in the rotators’s outside subtree, then this subtree will be taller than inside, and a single rotation(of the rotator) will correct the imbalance. Otherwise you must first rotate the root of the rotator’s inside subtree up into the rotator’s position and then rotate the node that replaced the rotator up into the pivot’s position. This is known as a double rotation.

  6. Keeping AVL trees balanced P R • Simple right rotation: When new item inserted in the left subtree of the left child B of the nearest ancestor A with balance factor +2 • Simple left rotation: When new item inserted in the right subtree of the right child B of the nearest ancestor A with balance factor –2: 2 0 P 1 0 0 D D R 0 P D 2 0 P 1 D 0 0 0 R R

  7. Keeping AVL trees balanced 2 P 3. Left-right rotation:When new item inserted in the right subtree of the left child B of the nearest ancestor A with balance factor of +2 4. Right-left rotation:When new item inserted in the left subtree of the right child B of the nearest ancestor A with balance factor -2 -1 G R 0 D H 1 M Left-right 0 H G P D M R

  8. Keeping AVL trees balanced • Each of these rotations performed by simply resetting some links • For example, consider a simple right rotation, used when item inserted in the left subtree of the left child B of the nearest ancestor A with balance factor +2 • A simple right rotation involves resetting three links. • Reset the link from the parent of A to B • Set the left link of A equal to the right link of B • Set the right link of B to point to A

  9. Insert Example Insert a 2 9 9 pivot 5 10 5 10 rotator 6 11 0 6 11 0 -1 1 -1 1 2

  10. AVL Left Rotate 9 9 pivot pivot 5 10 5 10 6 11 0 6 11 1 rotator -1 1 0 2 2 -1

  11. AVL Right Rotate 9 9 Right rotate 1 10 5 10 5 11 6 11 0 1 -1 2 6 0 2 -1

  12. Single Rotation Left /** * Rotate binary tree node with right child. i.e. Left rotate */ void SplayTree::rotateWithRightChild( BinaryNode * & k1 ) const { BinaryNode *k2 = k1->right; k1->right = k2->left; k2->left = k1; k1 = k2; } 2 k1 1 k2 1 A 2 C B C B A

  13. Another look at Double Rotation Left-right double 3 2 1 D 1 3 2 A C A B D C B

  14. AVL Rebalancing cost/benefit Balanced binary trees with n nodes will have a depth O(log2n) AVL trees thus guarantee search time will be O(log2n) Overhead involved in rebalancing as the AVL tree justified when search operations exceed insertions. Faster searches compensate for slower insertions. Empirical studies indicate that on the average, rebalancing is required for approximately 45 percent of the insertions. Roughly one-half of these rotations require a double rotation.

  15. Splay Trees A Splay Tree is also a BST Tree that is modified via a set of special rotations. This structure guarantees that any M consecutive tree operations starting from an empty tree take at most O(M log N) time. We call this an amortizedrunning time. Any single operation may take O(N) time although these occur infrequently. The basic idea of the splay tree is that after a node is accessed, it is pushed to the root by a series of AVL type rotations. Frequently accessed nodes are always near the top of the tree. "Self-adjusting Binary Search Trees", Sleator and Tarjan, JACM Volume 32, No 3, July 1985, pp 652-686.

  16. Zig Rotation P X P X C A A B B C Same as AVL single rotation

  17. Zig-Zag Rotation G X P D P G X A C A B D C B Same as AVL double rotation

  18. Zig-Zig Rotation G X P D P A X G C B A B D C

  19. Splaying at node 1 7 7 7 1 6 6 6 6 4 5 5 1 7 4 2 5 4 4 3 2 5 3 1 3 2 2 3 1

More Related