Docsity

AVL Trees: Balancing Binary Search Trees, Cheat Sheet of Computer Science

Toggle sidebar
bg1
7/9/2021
Data Structure and Algorithms - AVL Trees - Tutorialspoint
https://www.tutorialspoint.com/data_structures_algorithms/avl_tree_algorithm.htm
1/5
Data Structure and Algorithms - AVL Trees
Data Structure and Algorithms - AVL Trees
What if the input to binary search tree comes in a sorted (ascending or descending) manner? It
What if the input to binary search tree comes in a sorted (ascending or descending) manner? It
will then look like this โˆ’
will then look like this โˆ’
It is observed that BST's worst-case performance is closest to linear search algorithms, that is
It is observed that BST's worst-case performance is closest to linear search algorithms, that is
ฮŸ(n). In real-time data, we cannot predict data pattern and their frequencies. So, a need arises
ฮŸ(n). In real-time data, we cannot predict data pattern and their frequencies. So, a need arises
to balance out the existing BST.
to balance out the existing BST.
Named after their inventor
Named after their inventor Adelson
Adelson,
, Velski
Velski &
& Landis
Landis,
, AVL trees
AVL trees are height balancing binary
are height balancing binary
search tree. AVL tree checks the height of the left and the right sub-trees and assures that the
search tree. AVL tree checks the height of the left and the right sub-trees and assures that the
difference is not more than 1. This difference is called the
difference is not more than 1. This difference is called the Balance Factor
Balance Factor.
.
Here we see that the first tree is balanced and the next two trees are not balanced โˆ’
Here we see that the first tree is balanced and the next two trees are not balanced โˆ’
In the second tree, the left subtree of
In the second tree, the left subtree of C
C has height 2 and the right subtree has height 0, so the
has height 2 and the right subtree has height 0, so the
difference is 2. In the third tree, the right subtree of
difference is 2. In the third tree, the right subtree of A
A has height 2 and the left is missing, so it is
has height 2 and the left is missing, so it is
0, and the difference is 2 again. AVL tree permits difference (balance factor) to be only 1.
0, and the difference is 2 again. AVL tree permits difference (balance factor) to be only 1.
pf3
pf4
pf5

Partial preview of the text

Download AVL Trees: Balancing Binary Search Trees and more Cheat Sheet Computer Science in PDF only on Docsity!

Data Structure and Algorithms - AVL Trees

Data Structure and Algorithms - AVL Trees

What if the input to binary search tree comes in a sorted (ascending or descending) manner? It

What if the input to binary search tree comes in a sorted (ascending or descending) manner? It

will then look like this โˆ’

will then look like this โˆ’

It is observed that BST's worst-case performance is closest to linear search algorithms, that is

It is observed that BST's worst-case performance is closest to linear search algorithms, that is

ฮŸ(n). In real-time data, we cannot predict data pattern and their frequencies. So, a need arisesฮŸ(n). In real-time data, we cannot predict data pattern and their frequencies. So, a need arises

to balance out the existing BST.

to balance out the existing BST.

Named after their inventor

Named after their inventor Adelson

Adelson ,

Velski

Velski &

Landis

Landis ,

AVL trees

AVL trees are height balancing binary

are height balancing binary

search tree. AVL tree checks the height of the left and the right sub-trees and assures that the

search tree. AVL tree checks the height of the left and the right sub-trees and assures that the

difference is not more than 1. This difference is called the

difference is not more than 1. This difference is called the Balance Factor

Balance Factor .

Here we see that the first tree is balanced and the next two trees are not balanced โˆ’

Here we see that the first tree is balanced and the next two trees are not balanced โˆ’

In the second tree, the left subtree of

In the second tree, the left subtree of C

C

has height 2 and the right subtree has height 0, so the

has height 2 and the right subtree has height 0, so the

difference is 2. In the third tree, the right subtree of

difference is 2. In the third tree, the right subtree of A

A

has height 2 and the left is missing, so it is

has height 2 and the left is missing, so it is

0, and the difference is 2 again. AVL tree permits difference (balance factor) to be only 1.

0, and the difference is 2 again. AVL tree permits difference (balance factor) to be only 1.

BalanceFactorBalanceFactor = height(left-sutree) โˆ’ height(right-sutree)= height(left-sutree) โˆ’ height(right-sutree)

If the difference in the height of left and right sub-trees is more than 1, the tree is balanced usingIf the difference in the height of left and right sub-trees is more than 1, the tree is balanced using

some rotation techniques.

some rotation techniques.

AVL Rotations

AVL Rotations

To balance itself, an AVL tree may perform the following four kinds of rotations โˆ’

To balance itself, an AVL tree may perform the following four kinds of rotations โˆ’

Left rotation

Left rotation

Right rotation

Right rotation

Left-Right rotationLeft-Right rotation

Right-Left rotation

Right-Left rotation

The first two rotations are single rotations and the next two rotations are double rotations. To

The first two rotations are single rotations and the next two rotations are double rotations. To

have an unbalanced tree, we at least need a tree of height 2. With this simple tree, let's

have an unbalanced tree, we at least need a tree of height 2. With this simple tree, let's

understand them one by one.

understand them one by one.

Left Rotation

Left Rotation

If a tree becomes unbalanced, when a node is inserted into the right subtree of the right

If a tree becomes unbalanced, when a node is inserted into the right subtree of the right

subtree, then we perform a single left rotation โˆ’

subtree, then we perform a single left rotation โˆ’

In our example, node

In our example, node A

A

has become unbalanced as a node is inserted in the right subtree of A's

has become unbalanced as a node is inserted in the right subtree of A's

right subtree. We perform the left rotation by making

right subtree. We perform the left rotation by making A

A

the left-subtree of B.

the left-subtree of B.

Right Rotation

Right Rotation

AVL tree may become unbalanced, if a node is inserted in the left subtree of the left subtree.

AVL tree may become unbalanced, if a node is inserted in the left subtree of the left subtree.

The tree then needs a right rotation.

The tree then needs a right rotation.

State

State Action

Action

A node has been inserted into the right subtree of the left

A node has been inserted into the right subtree of the left

subtree. This makes

subtree. This makes C

C

an unbalanced node. These scenarios

an unbalanced node. These scenarios

cause AVL tree to perform left-right rotation.

cause AVL tree to perform left-right rotation.

We first perform the left rotation on the left subtree of

We first perform the left rotation on the left subtree of C

C

. This . This

makes makes AA , the left subtree of, the left subtree of BB ..

Node

Node C

C

is still unbalanced, however now, it is because of the left-

is still unbalanced, however now, it is because of the left-

subtree of the left-subtree.

subtree of the left-subtree.

We shall now right-rotate the tree, making

We shall now right-rotate the tree, making B

B

the new root node of

the new root node of

this subtree. this subtree. CC now becomes the right subtree of its own leftnow becomes the right subtree of its own left

subtree.

subtree.

The tree is now balanced.

The tree is now balanced.

Right-Left Rotation

Right-Left Rotation

The second type of double rotation is Right-Left Rotation. It is a combination of right rotation

The second type of double rotation is Right-Left Rotation. It is a combination of right rotation

followed by left rotation.

followed by left rotation.

State

State Action

Action

A node has been inserted into the left subtree of the right

A node has been inserted into the left subtree of the right

subtree. This makes

subtree. This makes A

A

, an unbalanced node with balance factor

, an unbalanced node with balance factor

First, we perform the right rotation along First, we perform the right rotation along CC node, makingnode, making CC thethe

right subtree of its own left subtree

right subtree of its own left subtree B

B

. Now, . Now, B

B

becomes the right

becomes the right

subtree of subtree of AA ..

Node

Node A

A

is still unbalanced because of the right subtree of its right

is still unbalanced because of the right subtree of its right

subtree and requires a left rotation. subtree and requires a left rotation.

A left rotation is performed by making

A left rotation is performed by making B

B

the new root node of the

the new root node of the

subtree.

subtree. A

A

becomes the left subtree of its right subtree

becomes the left subtree of its right subtree B

B

The tree is now balanced.

The tree is now balanced.