Ads Unit-2
Ads Unit-2
Binary Search Trees: Definition and Operations, AVL Trees: Definition and Operations,
Applications.
1.1INTRODUCTION
In a binary tree, every node can have a maximum of two children but there is no need to
maintain the order of nodes basing on their values. In a binary tree, the elements are arranged
in the order they arrive at the tree from top to bottom and left to right.
To enhance the performance of binary tree, we use a special type of binary tree known
as Binary Search Tree. Binary search tree mainly focuses on the search operation in a binary
tree. Binary search tree can be defined as follows...
Binary Search Tree is a binary tree in which every node contains only smaller values in
its left subtree and only larger values in its right subtree.
In a binary search tree, all the nodes in the left subtree of any node contains smaller values
and all the nodes in the right subtree of any node contains larger values as shown in the
following figure...
The following tree is a Binary Search Tree. In this tree, left subtree of every node contains
nodes with smaller values and right subtree of every node contains larger values.
Every binary search tree is a binary tree but every binary tree need not to be binary
search tree.
1. Searching become very efficient in a binary search tree since, we get a hint at each
step, about which sub-tree contains the desired element.
2. The binary search tree is considered as efficient data structure in compare to arrays
and linked lists. In searching process, it removes half sub-tree at every step. Searching
for an element in a binary search tree takes o(log 2 n) time. In worst case, the time it
takes to search an element is 0(n).
3. It also speed up the insertion and deletion operations as compare to that in array and
linked list.
Create the binary search tree using the following data elements.
43, 10, 79, 90, 12, 54, 11, 9, 50
1. Insert 43 into the tree as the root of the tree.
2. Read the next element, if it is lesser than the root node element, insert it as the root of
the left sub-tree.
The process of creating BST by using the given elements, is shown in the image
below.
Example2
10,12,5,4,20,8,7,15 and 13
Searching means finding or locating some specific element or node within a data structure.
However, searching for some specific node in binary search tree is pretty easy due to the fact
that, element in BST are stored in a particular order.
o Step 2: END
Insert function is used to add a new element in a binary search tree at appropriate location.
Insert function is to be designed in such a way that, it must node violate the property of
binary search tree at each value.
Delete function is used to delete the specified node from a binary search tree. However, we
must delete a node from a binary search tree in such a way, that the property of binary search
tree doesn't violate.
There are three situations of deleting a node from binary search tree.
a) The node to be deleted is a leaf node
It is the simplest case, in this case, replace the leaf node with the NULL and simple free the
allocated space.
In the following image, we are deleting the node 85, since the node is a leaf node, therefore
the node will be replaced with NULL and allocated space will be freed.
In this case, replace the node with its child and delete the child node, which now contains the
value which is to be deleted. Simply replace it with the NULL and free the allocated space.
In the following image, the node 12 is to be deleted. It has only one child. The node will be
replaced with its child node and the replaced node 12 (which is now leaf node) will simply be
deleted.
It is a bit complexed case compare to other two cases. However, the node which is to be
deleted, is replaced with its in-order successor or predecessor recursively until the node value
(to be deleted) is placed on the leaf of the tree. After the procedure, replace the node with
NULL and free the allocated space.
In the following image, the node 50 is to be deleted which is the root node of the tree. The in-
order traversal of the tree given below.
replace 50 with its in-order successor 52. Now, 50 will be moved to the leaf of the tree, which
will simply be deleted.
AVL tree is a height-balanced binary search tree. That means, an AVL tree is also a
binary search tree but it is a balanced tree. A binary tree is said to be balanced if, the
difference between the heights of left and right subtrees of every node in the tree is either -1,
0 or +1. In other words, a binary tree is said to be balanced if the height of left and right
children of every node differ by either -1, 0 or +1. In an AVL tree, every node maintains an
extra information known as balance factor. The AVL tree was introduced in the year 1962
by G.M. Adelson-Velsky and E.M. Landis.
An AVL tree is defined as follows...
An AVL tree is a balanced binary search tree. In an AVL tree, balance factor of every
node is either -1, 0 or +1.
Balance factor of a node is the difference between the heights of the left and right subtrees of
that node. The balance factor of a node is calculated either height of left subtree - height of
The above tree is a binary search tree and every node is satisfying balance factor condition.
So this tree is said to be an AVL tree.
Every AVL Tree is a binary search tree but every Binary Search Tree need not be AVL
tree.
In AVL tree, after performing operations like insertion and deletion we need to check
the balance factor of every node in the tree. If every node satisfies the balance factor
condition then we conclude the operation otherwise we must make it balanced. Whenever the
tree becomes imbalanced due to any operation we use rotation operations to make the tree
balanced.
Rotation operations are used to make the tree balanced.
Rotation is the process of moving nodes either to left or to right to make the tree
balanced.
There are four rotations and they are classified into two types.
In LL Rotation, every node moves one position to left from the current position. To
understand LL Rotation, let us consider the following insertion operation in AVL Tree...
In RR Rotation, every node moves one position to right from the current position. To
understand RR Rotation, let us consider the following insertion operation in AVL Tree...
The LR Rotation is a sequence of single left rotation followed by a single right rotation. In
LR Rotation, at first, every node moves one position to the left and one position to right from
the current position. To understand LR Rotation, let us consider the following insertion
operation in AVL Tree...
The RL Rotation is sequence of single right rotation followed by single left rotation. In RL
Rotation, at first every node moves one position to right and one position to left from the
current position. To understand RL Rotation, let us consider the following insertion operation
in AVL Tree...
1. Search
2. Insertion
3. Deletion
In an AVL tree, the search operation is performed with O(log n) time complexity. The search
operation in the AVL tree is similar to the search operation in a Binary search tree. We use
the following steps to search an element in AVL tree...
In an AVL tree, the insertion operation is performed with O(log n) time complexity. In AVL
Tree, a new node is always inserted as a leaf node. The insertion operation is performed as
follows...
Step 1 - Insert the new element into the tree using Binary Search Tree insertion logic.
Step 2 - After insertion, check the Balance Factor of every node.
Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for next
operation.
Step 4 - If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is
said to be imbalanced. In this case, perform suitable Rotation to make it balanced and
go for next operation.
The deletion operation in AVL Tree is similar to deletion operation in BST. But after every
deletion operation, we need to check with the Balance Factor condition. If the tree is balanced
after deletion go for next operation otherwise perform suitable rotation to make the tree
Balanced.
The two types of rotations are L rotation and R rotation. Here, we will discuss R rotations.
L rotations are the mirror images of them.
If the node which is to be deleted is present in the left sub-tree of the critical node, then L
rotation needs to be applied else if, the node which is to be deleted is present in the right sub-
tree of the critical node, the R rotation will be applied.
Let us consider that, A is the critical node and B is the root node of its left sub-tree. If node
X, present in the right sub-tree of A, is to be deleted, then there can be three different
situations:
If the node B has 0 balance factor, and the balance factor of node A disturbed upon deleting
the node X, then the tree will be rebalanced by rotating tree using R0 rotation.
The critical node A is moved to its right and the node B becomes the root of the tree with T1
as its left sub-tree. The sub-trees T2 and T3 becomes the left and right sub-tree of the node A.
the process involved in R0 rotation is shown in the following image.
Delete the node 30 from the AVL tree shown in the following image.
Solution
In this case, the node B has balance factor 0, therefore the tree will be
rotated by using R0 rotation as shown in the following image. The node
B(10) becomes the root, while the node A is moved to its right. The right
child of node B will now become the left child of node A.
Example
Delete Node 55 from the AVL tree shown in the following image.
Solution :
Deleting 55 from the AVL Tree disturbs the balance factor of the node 50 i.e. node A which
becomes the critical node. This is the condition of R1 rotation in which, the node A will be
moved to its right (shown in the image below). The right of B is now become the left of A
(i.e. 45).
R-1 rotation is to be performed if the node B has balance factor -1. This case is treated in the
same way as LR rotation. In this case, the node C, which is the right child of node B,
becomes the root node of the tree with B and A as its left and right children respectively.
The sub-trees T1, T2 becomes the left and right sub-trees of B whereas, T3, T4 become the
left and right sub-trees of A. The process involved in R-1 rotation is shown in the following
image.
Delete the node 60 from the AVL tree shown in the following image.
Solution:
in this case, node B has balance factor -1. Deleting the node 60, disturbs the balance factor of
the node 50 therefore, it needs to be R-1 rotated. The node C i.e. 45 becomes the root of the
tree with the node B(40) and A(50) as its left and right child.
1. Explain the AVL tree insertion and deletion with suitable example.
2. Describe the algorithms used to perform single and double rotation on AVL tree.
3. Explain about B+ trees with suitable algorithm.
4. How to insert and delete an element into a binary search tree and write down the code
for the insertion routine with an example.
5. What are binary search tree? Write an algorithm for deleting a node in a binary search
tree.
6. Create a binary search tree for the following numbers start from an empty binary
search tree. 45,26,10,60,70,30,40 Delete keys 10,60 and 45 one after the other and
show the trees at each