Presentation is loading. Please wait.

Presentation is loading. Please wait.

Balanced Trees AVL : Adelson-Velskii and Landis(1962)

Similar presentations


Presentation on theme: "Balanced Trees AVL : Adelson-Velskii and Landis(1962)"— Presentation transcript:

1 Balanced Trees AVL : Adelson-Velskii and Landis(1962)
CS 3013: Algorithms Balanced Trees AVL : Adelson-Velskii and Landis(1962)

2 Balanced Trees Balanced trees are those that are basically kept in a shape that allows search time to be log N either worst case or amortized over many searches. They may be weight balanced, height balanced , IPL influenced etc. They come in many flavors AVL trees (height) Red Black trees (height) Splay trees (amortized)

3 STL The implementation of choice for Set and Map in the standard template library is often the Red-Black tree. These are trees that have nodes defined as either red or black. These colored nodes are used to keep balance via an interesting algorithm. There is considerable discussion on AVL vrs RB see

4 Lets look at 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

5 Sample Trees -1 1 1 1 1 -1 2 2 -2 1 Not AVL trees 1

6 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:

7 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.

8 Keeping AVL trees balanced
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 P 1 D D R P D 2 P 1 D R R

9 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 D H 1 M Left-right H G P D M R

10 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

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

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

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

14 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 C B B A

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

16 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.

17 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 amortized running 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

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

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

20 Zig-Zig Rotation G X P D P A X G C B A B C D

21 Splaying at node 1 7 7 7 1 6 6 6 6 5 5 1 4 7 4 2 5 4 4 2 5 3 3 1 3 2 2 3 1

22


Download ppt "Balanced Trees AVL : Adelson-Velskii and Landis(1962)"

Similar presentations


Ads by Google