AVL Tree Rotations Daniel Box. Binary Search Trees A binary search tree is a tree created so that...
-
Upload
toby-forge -
Category
Documents
-
view
217 -
download
1
Transcript of AVL Tree Rotations Daniel Box. Binary Search Trees A binary search tree is a tree created so that...
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 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
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.
AVL trees
• For example, a binary search tree can be created that looks like this:
7
9
8
3
5
6
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
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.
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.
Single Right Rotation
A
T3
T4
T1
B
T2
Single Right Rotation
• When T4 was added, A became unbalanced to the left.
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
Single Right Rotation
• After performing the rotation, the tree looks like this:
• Tree is balanced.
B
A
T3T4
T1
T2
Code for Single Right Rotation
BinaryNode nodeC = A.getLeftChild();
A.setLeftChild(nodeC.getRightChild());
nodeC.setRightChild(A);
B = nodeC;
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.
Single Left Rotation
Single Left Rotation
• When T4 was added, A became unbalanced to the right.
A
B
T4
T2
T1
T3
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
Single Left Rotation
• After performing the rotation, the tree looks like this:
• Tree is balanced.
B
T3
T2T1
A
T4
Code for Single Left Rotation
BinaryNode nodeC = A.getRightChild();
A.setRightChild(nodeC.getLeftChild());
nodeC.setLeftChild(A)
B = nodeC;
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
Double Rotations
Double Rotations
• Any other kind of imbalance requires two rotations to be made.
Double Right Rotation
Double Right Rotation
• When T7 was added, A became unbalanced to the right.
A
C
T3
B
T1
T4
T7
T2
T5
T6
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.
Double Right Rotation
• After performing the rotation, the tree looks like this:
• Tree is balanced.
B
C
T2T1
A
T4
T7
T3
T5 T6
Code for Double Right Rotation
BinaryNode nodeC = A.getRightChild();
A.setRightChild(rotateRight(nodeC));
rotateLeft(A);
B = nodeC;
Code for Double Right Rotation
• A double right rotation is a single right rotation followed by a single left rotation.
Double Left Rotation
Double Left Rotation
• When T7 was added, A became unbalanced to the left.
A
T4
T2
T1
C
B
T3
T7
T5
T6
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.
Double Left Rotation
• After performing the rotation, the tree looks like this:
• Tree is balanced.
B
A
T2T1
C
T4
T7
T3
T5T6
Code for Double Left Rotation
BinaryNode nodeC = A.getLeftChild();
A.setLeftChild(rotateLeft(nodeC));
rotateRight(A);
B = nodeC;
Code for Double Left Rotation
• A double right rotation is a single left rotation followed by a single right rotation.
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.