0% found this document useful (0 votes)
21 views53 pages

DS Unit 3

The document discusses binary search trees and their operations. It explains how to construct, search, insert, and delete nodes from a binary search tree. It then describes AVL trees, which are balanced binary search trees, and covers rotations, insertion, deletion and other operations on AVL trees.

Uploaded by

BADMAN
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)
21 views53 pages

DS Unit 3

The document discusses binary search trees and their operations. It explains how to construct, search, insert, and delete nodes from a binary search tree. It then describes AVL trees, which are balanced binary search trees, and covers rotations, insertion, deletion and other operations on AVL trees.

Uploaded by

BADMAN
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/ 53

Data Structures

Unit-3
Advanced Trees
Binary Search Tree
● Binary Search Tree (BST) is a tree in which all the
nodes follow the below-mentioned properties −

● The value of the key of the left sub-tree is less than


the value of its parent (root) node's key.

● The value of the key of the right sub-tree is greater


than or equal to the value of its parent (root) node's
key.
Binary search tree
Binary search tree
● Construct BST for 56,34,67,23,98,45,12,76,85,90,25
● Construct BST for 63,35,54,28,90,14,59,76,10,88,7,98
Binary search tree
▪Basic Operations
▪Following are the basic operations of a tree −
▪Search − Searches an element in a tree.
▪Insert − Inserts an element in a tree.
▪Pre-order Traversal − Traverses a tree in a pre-order manner.
▪In-order Traversal − Traverses a tree in an in-order manner.
▪Post-order Traversal − Traverses a tree in a post-order
Binary search tree-Insertion
●A new key is always inserted at leaf. We start searching a key from root till we hit
a leaf node. Once a leaf node is found, the new node is added as a child of the
leaf node
Insert()
{
n=(struct node*)malloc (sizeof(struct node));
Pf(“enter dat”)
Scanf(“%d”,&n->data);
n->left=NULL;
n->right=NULL;
If(root==NULL)
root=n;
else
{
Temp=root;
While(temp!=NULL)
{
if(n->data<temp->data)
{
if(temp->left==NULL)
{temp->left= n;
break;
}
else
temp=temp->left
}
else
{
if(temp->right==NULL)
{
temp->right=n;
break;
}
else
temp=temp->right
}
}}}
Binary search tree-Search
1. Start from root.
2. Compare the inserting element with root, if less than root, then
recurse for left, else recurse for right.
3. If element to search is found anywhere, return true, else return
false.
Search()
{
If(root==NULL)
Printf(“tree is empty”)
Else
{
Printf(“enter ele to search”)
Scanf(“%d”,&key);
Temp=root;
While(temp!=NULL)
{
If(key<temp->data)
Temp=temp->left;
Else if(key>temp->data)
Temp=temp->right;
Else if(key==temp->data)
{
Printf(“element is found”);
Break;
}}
If(temp==NULL)
Printf(“ele not found”)
}
Binary search tree-Deletion
▪ There exists three
cases
▪ Case 1: Node
with zero
children: this is
the easiest
situation, you just
need to delete the
node which has
no further
Binary search tree-Deletion
Case 2 - Node
with one child:
once you
delete the
node, simply
connect its
child node
with the parent
node of the
deleted value.
Binary search tree-Deletion
▪ Case 3 Node with two
children: this is the most
difficult situation, and it works
on the following two rules
▪ 3a - In Order Predecessor: to
delete the node with two
children and replace it with the
largest value on the left-
subtree of the deleted node
Binary search tree-Deletion
▪ 3b - In Order
Successor: to
delete the node
with two children
and replace it
with the Smallest
value in the
right-subtree of
the deleted node
Del()
{int key,c=0;
Struct node *parent=NULL;
If(root==NULL)
Printf(“Tree is empty”);
Else
{
Printf(“enter ele to del”);
Scanf(“%d”,&key);
Temp=root;
While(temp!=NULL)
{
if(key<temp->data)
{ parent=temp;
temp=temp->left;

}
Else if(temp->left!=NULL && temp->right==NULL)//only left child
{
if(parent->left==temp)
parent->left=temp->left;
else
Parent->right=temp->left;
Free(temp);C++;
Break;
}
Else if ( temp->left==NULL && temp->right!=NULL)//only right child
{
if(parent->left==temp)
Parent->left=temp->right;
Else
Parent->right=temp->right;
Free(temp);C++
Else if(temp->left!=NULL && temp->right!=NULL)//two children
{
t=temp->right;
P=NULL;
While(t->left!=NULL)
{
P=t;
t=t->left;
}
Temp->data=t->data;
If(t->right!=NULL)
{
P->left=t->right;
}
Free(t);C++;
Break;
}
Void inorder(struct node *temp)
{
If(temp!=NULL)
{
Inorder(temp->left);
Printf(“%d”,temp->data)
Inorder(temp->right);
}
AVL Tree
▪ The AVL tree was introduced in the year 1962 by G.M.
Adelson-Velsky and E.M. Landis.
▪ 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 an AVL tree, every node maintains an extra information
known as balance factor.
▪ Balance factor = heightOfLeftSubtree - heightOfRightSubtree
AVL Tree
▪ Example of AVL Tree
AVL Tree Rotations
▪ 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.
AVL Tree Rotations
▪ There are four rotations and they are classified into two types.
Single Left Rotation (LL Rotation)
▪ 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...
Single Right Rotation (RR Rotation)
▪ 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...
Left Right Rotation (LR Rotation)
▪ 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...
Right Left Rotation (RL Rotation)
▪ 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...
Operations on an AVL Tree
▪ The following operations are performed on AVL tree...
▪ Search
▪ Insertion
▪ Deletion
Search Operation in AVL Tree
▪ 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...
▪ Step 1 - Read the search element from the user.
▪ Step 2 - Compare the search element with the value of root node in the tree.
▪ Step 3 - If both are matched, then display "Given node is found!!!" and terminate the
function
▪ Step 4 - If both are not matched, then check whether search element is smaller or
larger than that node value.
▪ Step 5 - If search element is smaller, then continue the search process in left subtree.
▪ Step 6 - If search element is larger, then continue the search process in right subtree.
▪ Step 7 - Repeat the same until we find the exact element or until the search element
is compared with the leaf node.
▪ Step 8 - If we reach to the node having the value equal to the search value, then
display "Element is found" and terminate the function.
▪ Step 9 - If we reach to the leaf node and if it is also not matched with the search
Insertion Operation 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.
Construct an AVL Tree by inserting numbers from 1 to 8.
Deletion Operation in AVL Tree
▪ 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.
▪ Deleting a node from an AVL tree is similar to that in a binary
search tree. Deletion may disturb the balance factor of an AVL
tree and therefore the tree needs to be rebalanced in order to
maintain the AVLness.
Deletion Operation in AVL Tree
▪ To rebalance the tree there are two types of rotations
▪ 1.L rotation
▪ 2.R rotation
▪ 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.
Deletion Operation in AVL Tree
▪ 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
▪ 1.R0 rotation (Node B has balance factor 0 )
▪ 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.
1.R0 rotation
Example For R0 rotation

Delete the node 30 from the AVL tree


R1 Rotation (Node B has balance factor 1)

▪ R1 Rotation is to be performed if the balance factor of Node B


is 1
Example For R1 rotation

Delete Node 55
from the AVL tree
R-1 Rotation (Node B has balance factor -1)

▪ 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.
Example For R1 rotation

Deleting
the node
60
m-WAY Search Trees
▪ The m-way search trees are multi-way trees which are
generalised versions of binary trees where each node contains
multiple elements.
▪ In an m-Way tree of order m, each node contains a maximum of
m – 1 elements and m children.
▪ An m-way search tree is a m-way tree in which following
condition should be satisfied −
▪ Each node is associated with m children and m-1 key fields
▪ The keys in each node are arranged in ascending order.
▪ The keys in the first j children are less than the j-th key.
▪ The keys in the last m-j children are higher than the j-th key.
Structure of M-Way Search Tree

▪In the above structure, P0, P1 …, Pn are the pointers to


the node’s sub-trees and K0, k1, …, Kn-1 are the key
values of the node.
▪All the key values are stored in ascending order.
B - Tree
▪ B-Tree is a self-balanced search tree in which every node
contains multiple keys and has more than two children.
▪ B-Tree of Order m has the following properties...
▪ All leaf nodes must be at same level.
▪ All nodes except root must have at least [m/2]-1 keys and maximum of
m-1 keys.
▪ All non leaf nodes except root (i.e. all internal nodes) must have at least
m/2 children.
▪ If the root node is a non leaf node, then it must have atleast 2 children.
▪ A non leaf node with n-1 keys must have n number of children.
▪ All the key values in a node must be in Ascending Order.
Example
B+Tree
▪ B+ tree eliminates the drawback B-tree used for indexing by
storing data pointers only at the leaf nodes of the tree
▪ The B+ Trees are extended version of B-Trees
▪ In B+ tree records, can be stored at the leaf node, internal nodes
will store the key values only.
Advantages of B+ Tree over B-Tree
▪ Records can be fetched in equal number of disk accesses
▪ Height of the tree remains balanced, and less as compared
to B-Trees
▪ As the leafs are connected like linked list, we can search
elements in sequential manner also
▪ Keys are used for indexing
▪ The searching is faster, as data are stored at leaf level only.
Red Black Trees
▪ A Red Black Tree is a type of self-balancing binary search tree,
in which every node is colored with a red or black
▪ Properties of Red Black Tree
▪ The root node should always be black in color.
▪ Every null child of a node is black in red black tree.
▪ The children of a red node are black. It can be possible that
parent of red node is black node.
▪ All the leaves have the same black depth.
▪ Every simple path from the root node to the (downward)
leaf node contains the same number of black nodes.
Example of Red-black tree
Advantages of Red Black Tree
▪ AVL Trees are more balanced than the Red-Black tree. But
the major disadvantage is, there will be more rotations
during insertion and deletion. For multiple insertion and
deletion, Red-Black tree will be helpful.

You might also like