Presentation is loading. Please wait.

Presentation is loading. Please wait.

AVL Tree Rotations Daniel Box. Binary Search Trees A binary search tree is a tree created so that all of the items in the left subtree of a node are less.

Similar presentations


Presentation on theme: "AVL Tree Rotations Daniel Box. Binary Search Trees A binary search tree is a tree created so that all of the items in the left subtree of a node are less."— Presentation transcript:

1 AVL Tree Rotations Daniel Box

2 Binary Search Trees A binary search tree is a tree created so that all of the items in the left subtree of a node are less than (or equal to) the value of that node, and all of the items in the right subtree of a node are greater than (or equal to) the value of that node

3 AVL trees An AVL tree is a special type of binary search tree. In an AVL tree, the height of the left subtree is not allowed to differ from the height of the right subtree by more than 1. This makes searches for elements faster.

4 AVL trees For example, a binary search tree can be created that looks like this:

5 AVL trees An AVL tree containing the same data inserted in the same order looks like this: It is much faster to find the node 3 in this tree than in a regular binary search tree

6 AVL trees To keep the balance of an AVL tree correct, rotations must be performed when the tree becomes unbalanced. There is a specific type of rotation to perform for each different way that the tree can become unbalanced.

7 Types of rotations There are 4 types of AVL tree rotations Single Right Rotation Single Left Rotation Double Right Rotation Double Left Rotation For each following example, A is the node that becomes unbalanced. Node A can have parents above it, but those parents are not affected by the rotation, except for having to redirect one child pointer.

8 Single Right Rotation

9 A T3 T4 T1 B T2 Single Right Rotation When T4 was added, A became unbalanced to the left.

10 Single Right Rotation A is unbalanced to the left B’s left subtree is bigger than its right subtree (left-heavy) This means we must perform a single right rotation. A T3 T4 T1 B T2

11 Single Right Rotation After performing the rotation, the tree looks like this: Tree is balanced. B A T3T4 T1 T2

12 Code for Single Right Rotation BinaryNode nodeC = A.getLeftChild(); A.setLeftChild(nodeC.getRightChild()); nodeC.setRightChild(A); B = nodeC;

13 Code for Single Right Rotation B and A are rotated to the right T2 is moved from the left child of B to the right child of A.

14 Single Left Rotation

15 When T4 was added, A became unbalanced to the right. A B T4 T2 T1 T3

16 Single Left Rotation A is unbalanced to the right B’s right subtree is bigger than its left subtree (right-heavy) This means we must perform a single left rotation. A B T4 T2 T1 T3

17 Single Left Rotation After performing the rotation, the tree looks like this: Tree is balanced. B T3 T2T1 A T4

18 Code for Single Left Rotation BinaryNode nodeC = A.getRightChild(); A.setRightChild(nodeC.getLeftChild()); nodeC.setLeftChild(A) B = nodeC;

19 Code for Single Left Rotation B and A are rotated to the left T2 is moved from the left child of B to the right child of A

20 Double Rotations

21 Any other kind of imbalance requires two rotations to be made.

22 Double Right Rotation

23 When T7 was added, A became unbalanced to the right. A C T3 B T1 T4 T7 T2 T5 T6

24 Double Right Rotation A is unbalanced to the right. B’s right subtree is bigger than its left subtree (right-heavy) C’s left subtree is bigger that its right subtree (left-heavy) This means we must perform a double right rotation.

25 Double Right Rotation After performing the rotation, the tree looks like this: Tree is balanced. B C T2T1 A T4 T7 T3 T5T6

26 Code for Double Right Rotation BinaryNode nodeC = A.getRightChild(); A.setRightChild(rotateRight(nodeC)); rotateLeft(A); B = nodeC;

27 Code for Double Right Rotation A double right rotation is a single right rotation followed by a single left rotation.

28 Double Left Rotation

29 When T7 was added, A became unbalanced to the left. A T4 T2 T1 C B T3 T7 T5 T6

30 Double Left Rotation A is unbalanced to the left B’s left subtree is bigger than its right subtree (left-heavy) C’s right subtree is bigger than its left subtree (right-heavy) This means we must perform a double left rotation.

31 Double Left Rotation After performing the rotation, the tree looks like this: Tree is balanced. B A T2T1 C T4 T7 T3 T5T6

32 Code for Double Left Rotation BinaryNode nodeC = A.getLeftChild(); A.setLeftChild(rotateLeft(nodeC)); rotateRight(A); B = nodeC;

33 Code for Double Left Rotation A double right rotation is a single left rotation followed by a single right rotation.

34 Summary AVL trees allow for faster searches for information by rotating the tree when one side becomes much longer than the other. AVL trees have 4 different rotations they can apply to maintain the necessary balance. For any possible imbalance, one of these rotations will rebalance the tree.


Download ppt "AVL Tree Rotations Daniel Box. Binary Search Trees A binary search tree is a tree created so that all of the items in the left subtree of a node are less."

Similar presentations


Ads by Google