AVL Tree Rotations Daniel Box. Binary Search Trees A binary search tree is a tree created so that...

34
AVL Tree Rotations Daniel Box

Transcript of AVL Tree Rotations Daniel Box. Binary Search Trees A binary search tree is a tree created so that...

Page 1: 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.

AVL Tree Rotations

Daniel Box

Page 2: 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.

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.

7

9

83

5

6

Page 3: 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.

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.

Page 4: 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.

AVL trees

• For example, a binary search tree can be created that looks like this:

7

9

8

3

5

6

Page 5: 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.

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

8

73

5

9

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

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.

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

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.

Page 8: 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.

Single Right Rotation

Page 9: 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.

A

T3

T4

T1

B

T2

Single Right Rotation

• When T4 was added, A became unbalanced to the left.

Page 10: 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.

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

Page 11: 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.

Single Right Rotation

• After performing the rotation, the tree looks like this:

• Tree is balanced.

B

A

T3T4

T1

T2

Page 12: 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.

Code for Single Right Rotation

BinaryNode nodeC = A.getLeftChild();

A.setLeftChild(nodeC.getRightChild());

nodeC.setRightChild(A);

B = nodeC;

Page 13: 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.

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.

Page 14: 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.

Single Left Rotation

Page 15: 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.

Single Left Rotation

• When T4 was added, A became unbalanced to the right.

A

B

T4

T2

T1

T3

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

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

Page 17: 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.

Single Left Rotation

• After performing the rotation, the tree looks like this:

• Tree is balanced.

B

T3

T2T1

A

T4

Page 18: 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.

Code for Single Left Rotation

BinaryNode nodeC = A.getRightChild();

A.setRightChild(nodeC.getLeftChild());

nodeC.setLeftChild(A)

B = nodeC;

Page 19: 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.

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

Page 20: 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.

Double Rotations

Page 21: 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.

Double Rotations

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

Page 22: 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.

Double Right Rotation

Page 23: 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.

Double Right Rotation

• When T7 was added, A became unbalanced to the right.

A

C

T3

B

T1

T4

T7

T2

T5

T6

Page 24: 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.

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.

Page 25: 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.

Double Right Rotation

• After performing the rotation, the tree looks like this:

• Tree is balanced.

B

C

T2T1

A

T4

T7

T3

T5 T6

Page 26: 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.

Code for Double Right Rotation

BinaryNode nodeC = A.getRightChild();

A.setRightChild(rotateRight(nodeC));

rotateLeft(A);

B = nodeC;

Page 27: 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.

Code for Double Right Rotation

• A double right rotation is a single right rotation followed by a single left rotation.

Page 28: 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.

Double Left Rotation

Page 29: 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.

Double Left Rotation

• When T7 was added, A became unbalanced to the left.

A

T4

T2

T1

C

B

T3

T7

T5

T6

Page 30: 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.

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.

Page 31: 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.

Double Left Rotation

• After performing the rotation, the tree looks like this:

• Tree is balanced.

B

A

T2T1

C

T4

T7

T3

T5T6

Page 32: 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.

Code for Double Left Rotation

BinaryNode nodeC = A.getLeftChild();

A.setLeftChild(rotateLeft(nodeC));

rotateRight(A);

B = nodeC;

Page 33: 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.

Code for Double Left Rotation

• A double right rotation is a single left rotation followed by a single right rotation.

Page 34: 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.

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.