0% found this document useful (0 votes)
160 views

Module 4.5(AVL Trees)

An AVL tree is a self-balancing binary search tree that maintains balance factors of -1, 0, or 1 for each node to ensure efficient operations. Insertion and deletion in an AVL tree may require rotations to restore balance, with four types of rotations (LL, RR, LR, RL) used depending on the structure after an operation. The document also briefly discusses binary heaps, which are complete binary trees that maintain a heap property for priority queue implementations.

Uploaded by

pradeepshettar50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views

Module 4.5(AVL Trees)

An AVL tree is a self-balancing binary search tree that maintains balance factors of -1, 0, or 1 for each node to ensure efficient operations. Insertion and deletion in an AVL tree may require rotations to restore balance, with four types of rotations (LL, RR, LR, RL) used depending on the structure after an operation. The document also briefly discusses binary heaps, which are complete binary trees that maintain a heap property for priority queue implementations.

Uploaded by

pradeepshettar50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

AVL Trees

AVL tree is a self-balancing binary search tree invented by G.M. Adelson-


Velsky and E.M. Landis in 1962. The tree is named AVL in honour of its
inventors.
The structure of an AVL tree is the same as that of a binary search tree but
with a little difference.
In its structure, it stores an additional variable called the BalanceFactor. Thus,
every node has a balance factor associated with it.
The balance factor of a node is calculated by subtracting the height of its
right sub-tree from the height of its left sub-tree.
A binary search tree in which every node has a balance factor of –1, 0, or 1 is
said to be height balanced.
A node with any other balance factor is considered to be unbalanced and
requires rebalancing of the tree.
Balance factor = Height (left sub-tree) – Height (right sub-tree)

 If the balance factor of a node is 1, then it


means that the left sub-tree of the tree is
one level higher than that of the right sub-
tree. Such a tree is therefore called as a
left-heavy tree.
 If the balance factor of a node is –1, then it
means that the left sub-tree of the tree is
one level lower than that of the right sub-
tree. Such a tree is therefore called as a
right-heavy tree.
 If the balance factor of a node is 0, then it
means that the height of the left sub-tree
(longest path in the left sub-tree) is equal
to the height of the right sub-tree
(Balanced Tree).
Operations on AVL Trees

Searching for a Node in an AVL Tree

Inserting a New Node in an AVL Tree

Deleting a Node from an AVL Tree


Searching for a Node in an AVL Tree

Searching in an AVL tree is performed exactly the same way as it is


performed in a binary search tree.
Due to the height-balancing of the tree, the search operation takes O(log n)
time to complete.
Since the operation does not modify the structure of the tree, no special
provisions are required.
Inserting a New Node in an AVL Tree
 Insertion in an AVL tree is also done in the same way as it is done in a
binary search tree.
 In the AVL tree, the new node is always inserted as the leaf node.
 But the step of insertion is usually followed by an additional step of
rotation.
 Rotation is done to restore the balance of the tree.
 However, if insertion of the new node does not disturb the balance
factor, that is, if the balance factor of every node is still –1, 0, or 1, then
rotations are not required.
 During insertion, the new node is inserted as the leaf node, so it will always
have a balance factor equal to zero.
 The only nodes whose balance factors will change are those which lie in the
path between the root of the tree and the newly inserted node.
 The possible changes which may take place in any node on the path are as
follows:
 Initially, the node was either left- or right-heavy and after insertion, it becomes
balanced.
 Initially, the node was balanced and after insertion, it becomes either left- or
right-heavy.
 Initially, the node was heavy (either left or right) and the new node has been
inserted in the heavy sub-tree, thereby creating an unbalanced sub-tree. Such a
node is said to be a critical node.
• If we insert a new node with the value 30, then the new tree will still be
balanced and no rotations will be required in this case. Look at the tree given in
Figure which shows the tree after inserting node 30
• Let us take another example to see how insertion can disturb the balance factors
of the nodes and how rotations are done to restore the AVL property of a tree

Consider this figure and insert a new node with Note that there are three nodes in the tree that have
the value 71 and the new tree will be….. their balance factors 2, –2, and –2, thereby
disturbing the AVLness of the tree. So, here comes the
need to perform rotation.
To perform rotation:
• first task is to find the critical node. Critical node is the nearest ancestor node on
the path from the inserted node to the root whose balance factor is neither –1, 0,
nor 1. In the tree given above, the critical node is 72. The
• second task in rebalancing the tree is to determine which type of rotation has to
be done.
The four categories of rotations are:
LL rotation The new node is inserted in the left sub-tree of the left sub-tree of the
critical node.
RR rotation The new node is inserted in the right sub-tree of the right sub-tree of the
critical node.
LR rotation The new node is inserted in the right sub-tree of the left sub-tree of the
critical node.
RL rotation The new node is inserted in the left sub-tree of the right sub-tree of the
critical node
LL rotation
• Tree (a) is an AVL tree. In tree (b), a
new node is inserted in the left sub-tree
of the left sub-tree of the critical node A
(node A is the critical node because it is
the closest ancestor whose balance
factor is not –1, 0, or 1), so we apply LL
rotation as shown in tree (c).

• While rotation, node B becomes the


root, with T1 and A as its left and right
child. T2 and T3 become the left and
right sub-trees of A.
RR rotation
Tree (a) is an AVL tree. In tree (b), a
new node is inserted in the right
sub-tree of the right sub-tree of the
critical node A (node A is the
critical node because it is the
closest ancestor whose balance
factor is not –1, 0, or 1), so we
apply RR rotation as shown in tree
(c).
Note that the new node has now
become a part of tree T3 . While
rotation, node B becomes the root,
with A and T3 as its left and right
child. T1 and T2 become the left
and right sub-trees of A.
LR rotation
 Tree (a) is an AVL tree. In tree (b), a new
node is inserted in the right sub-tree of the
left sub-tree of the critical node A.
 so we apply LR rotation as shown in tree (c).
Note that the new node has now become a
part of tree T2 . While rotation, node C
becomes the root, with B and A as its left
and right children. Node B has T1 and T2 as
its left and right sub-trees and T3 and T4
become the left and right sub-trees of node
A.
3-1=2
2nd:right shift of left subtree
Insert the value 39
45

1 3-1=2 0
2-1=1
45 45 37 63

0 1-2=-1 0 1
0
36 63 36 63 39
36
0

0 0-1=-1 0
0 37 27
0 37
27 27
1st : Left shift of right subtree
0
39
0
37

1
0 Now it’s a Balanced tree, this LR rotation have two
36 45 rotation so it is also called as double rotation

0
0 39 0
27 39
RL rotation
 Tree (a) is an AVL tree. In tree (b), a
new node is inserted in the left sub-
tree of the right sub-tree of the
critical node A.
 so we apply RL rotation as shown in
tree (c). Note that the new node has
now become a part of tree T2 . While
rotation, node C becomes the root,
with A and B as its left and right
children. Node A has T1 and T2 as its
left and right sub-trees and T3 and T4
become the left and right sub-trees of
node B.
Insert node with the value 92 for the given tree.
Example : Construct an AVL tree by inserting the following elements in the given order. 63,
9, 19, 27, 18, 108, 99, 81.
Example: Construct an AVL tree by inserting the following elements in the
given 14, 17, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20.

Create an AVL tree using the following sequence of data: 16, 27, 9, 11, 36, 54,
81, 63, 72.
Deleting a Node from an AVL Tree
• Deletion of a node in an AVL tree is similar to that of binary search trees.

• But it goes one step ahead.

• Deletion may disturb the AVLness of the tree, so to rebalance the AVL tree,
we need to perform rotations.

• There are two classes of rotations that can be performed on an AVL tree
after deleting a given node. These rotations are R rotation and L rotation.

• There are three categories of L and R rotations.


On deletion of node X from the AVL tree, if node A becomes the critical
node (closest ancestor node on the path from X to the root node that does
not have its balance factor as 1, 0, or –1), then the type of rotation depends
on whether X is in the left sub-tree of A or in its right sub-tree.
If the node to be deleted is present in the left sub-tree of A, then L rotation
is applied, else if X is in the right sub-tree, R rotation is performed.
• The variations of L rotation are L–1, L0, and L1 rotation.

• R rotation, there are R0, R–1, and R1 rotations.


R0 Rotation

• Let B be the root of the left or right sub-tree of A (critical node). R0 rotation is applied if the
balance factor of B is 0. This is illustrated in Fig.

• Tree (a) is an AVL tree. In tree (b), the node X is to be deleted from the right sub-tree of the
critical node A.

• Since the balance factor of node B is 0, we apply R0 rotation as shown in tree (c). During the
process of rotation, node B becomes the root, with T1 and A as its left and right child. T2 and T3
become the left and right sub-trees of A.
Example : Consider the AVL tree given in Fig. and delete 72 from it.
R1 Rotation

• Let B be the root of the left or right sub-tree of A (critical node).


• R1 rotation is applied if the balance factor of B is 1.
• Observe that R0 and R1 rotations are similar to LL rotations; the only difference
is that R0 and R1 rotations yield different balance factors.
• Tree (a) is an AVL tree. In tree (b), the node X is to be deleted from the right sub-tree of
the critical node A.
• Since the balance factor of node B is 1, we apply R1 rotation as shown in tree (c).
• During the process of rotation, node B becomes the root, with T1 and A as its left and
right children. T2 and T3 become the left and right sub-trees of A.
Example : Consider the AVL tree given in Fig. and delete 72 from it.
R–1 Rotation

• Let B be the root of the left or right sub-tree of A (critical node).


• R–1 rotation is applied if the balance factor of B is –1. Observe that R–1 rotation is
similar to LR rotation.
• Tree (a) is an AVL tree. In tree (b), the node X is to be deleted from the right sub-tree
of the critical node A.
• Since the balance factor of node B is –1, we apply R–1 rotation as shown in tree (c).
• While rotation, node C becomes the root, with T1 and A as its left and right child. T2 and T3
become the left and right sub-trees of A.
Example : Consider the AVL tree given in Fig. and delete 72 from it.
Example : Delete nodes 52, 36, and 61 from the AVL tree given in Fig.
Assignment

1. Create a binary search tree with the input given below: 98, 2, 48, 12, 56, 32, 4, 67, 23,
87, 23, 55, 46
(a) Insert 21, 39, 45, 54, and 63 into the tree
(b) Delete values 23, 56, 2, and 45 from the tree
Binary Heap
• A binary heap is a complete binary tree in which every node satisfies the heap
property which states that:

 If B is a child of A, then key(A) ≥ key(B)

• This implies that elements at every node will be either greater than or equal to the
element at its left and right child. Thus, the root node has the highest key value in
the heap. Such a heap is commonly known as a max-heap.

• elements at every node will be either less than or equal to the element at its left
and right child. Thus, the root has the lowest key value. Such a heap is called a min-
heap.
It follows the same rules as that of a complete binary tree. That is, if an
element is at position i in the array, then its left child is stored at position 2i
and its right child at position 2i+1.
Being a complete binary tree, all the levels of the tree except the last level
are completely filled.
The height of a binary tree is given as log2 n, where n is the number of
elements.
Heaps (also known as partially ordered trees) are a very popular data
structure for implementing priority queues
Inserting a New Element in a Binary Heap
• Consider a max heap H with n elements. Inserting a new value into the heap is done in
the following two steps:

1. Add the new value at the bottom of H in such a way that H is still a complete binary
tree but not necessarily a heap.

2. Let the new value rise to its appropriate place in H so that H now becomes a heap as
well.

• To do this, compare the new value with its parent to check if they are in the correct order.

• If they are, then the procedure halts, else the new value and its parent’s value are
swapped and Step 2 is repeated
Example : Consider the max heap given in Fig. and insert 99 in it.
Example : Build a max heap H from the given set of numbers: 45, 36, 54, 27,
63, 72, 61, and 18.
Memory representation of the heap.
Algorithm to insert an element in a max heap
 We assume that H with n elements is stored in
array HEAP. VAL has to be inserted in HEAP. The
location of VAL as it rises in the heap is given by
POS, and PAR denotes the location of the parent of
VAL.
 Note that this algorithm inserts a single value in
the heap. In order to build a heap, use this
algorithm in a loop. For example, to build a heap
with 9 elements, use a for loop that executes 9
times and in each pass, a single value is inserted.
 The complexity of this algorithm in the average
case is O(1). This is because a binary heap has
O(log n) height. Since approximately 50% of the
elements are leaves and 75% are in the bottom
two levels, the new element to be inserted will
only move a few levels upwards to maintain the
heap.
In the worst case, insertion of a single value may take
O(log n) time and, similarly, to build a heap of n
elements, the algorithm will execute in O(n log n) time
Deleting an Element from a Binary Heap
Consider a max heap H having n elements. An element is always deleted from the root of the
heap. So, deleting an element from the heap is done in the following three steps:

1. Replace the root node’s value with the last node’s value so that H is still a complete binary tree
but not necessarily a heap.

2. Delete the last node.

3. Sink down the new root node’s value so that H satisfies the heap property. In this step,
interchange the root node’s value with its child node’s value (whichever is largest among its
children).

Here, the value of root node = 54 and the value of the last node = 11. So, replace 54 with 11 and
delete the last node.
Example : Consider the max heap H shown in Fig. and delete the root node’s
value
Algorithm to delete the root element from a max heap

We assume that heap H with n elements is stored


using a sequential array called HEAP. LAST is the last
element in the heap and PTR, LEFT, and RIGHT denote
the position of LAST and its left and right children
respectively as it moves down the heap.
Applications of Binary Heaps

Binary heaps are mainly applied for


1. Sorting an array using heapsort algorithm.
2. Implementing priority queues.
Binary Heap Implementation of Priority Queues
 A priority queue is similar to a queue in which an item is
dequeued (or removed) from the front. However, unlike
a regular queue, in a priority queue the logical order of
elements is determined by their priority.
 While the higher priority elements are added at the front
of the queue, elements with lower priority are added at
the rear.
 Consider the figure In this bag you can insert any priority
but you can take out one with the highest value. Though
we can easily implement priority queues using a linear
array, but we should first consider the time required to
insert an element in the array and then sort it.
 Therefore, a better way to implement a priority queue is
by using a binary heap which allows both enqueue and
dequeue of elements in O(log n) time.

You might also like