0% found this document useful (0 votes)
37 views12 pages

Dsa Mod4 - Part2

Dsa Mod4 - Part2

Uploaded by

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

Dsa Mod4 - Part2

Dsa Mod4 - Part2

Uploaded by

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

Data Structures and Applications (CS204) Module 4

4.6 Binary Search Tree


In a binary tree, every node can have maximum of two children but there is no order of nodes based on their
values. In binary tree, the elements are arranged as they arrive to the tree, from top to bottom and left to right.
To enhance the performance of binary tree, we use special type of binary tree known as Binary Search Tree.
Binary search tree mainly focus on the search operation in 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 sub tree
and only larger values in its right sub tree.

Example
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 all the Binary Trees need not to be binary search trees.

Example
Construct a Binary Search Tree by inserting the following sequence of numbers...
10, 12, 5, 4, 20, 8, 7, 15 and 13

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 1


Data Structures and Applications (CS204) Module 4

Operations on a Binary Search Tree


The following operations are performed on a binary search tree...

 Search

 Insertion

 Deletion

 Traversal

Search Operation in BST


The searchoperation is performed as follows...
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 matching, then display "Given node found!!!" and terminate the function
Step 4: If both are not matching, 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 exact element or we completed with a leaf node
Step 8: If we reach the node with search value, then display "Element is found" and terminate the function.
Step 9: If we reach a leaf node and it is also not matching, then display "Element not found" and terminate
the function.

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 2


Data Structures and Applications (CS204) Module 4

void search(node *root, int key)


{
if (root == NULL)
{
printf("key not found\n");
return;
}
if(root->data == key)
{
printf("key found\n");
return;
}
if (key<root->data)
search(root->left, key);
else
search(root->right, key);
}

Insertion Operation in BST


In binary search tree, new node is always inserted as a leaf node. The insertion operation is performed as
follows...
Step 1: Create a newNode with given value and set its left and right to
NULL.Step 2: Check whether tree is Empty.
Step 3: If the tree is Empty, then set set root to newNode.
Step 4: If the tree is Not Empty, then check whether value of newNode is smaller or larger than the
node(here it is root node).
Step 5: If newNode is smaller than or equal to the node, then move to its left child. If newNode is larger
thanthe node, then move to its right child.
Step 6: Repeat the above step until we reach to a leaf node (e.i., reach to NULL).
Step 7: After reaching a leaf node, then insert the newNode as left child if newNode is smaller or equal
to that leaf else insert it as right child.
node* insert(node *root,int key)
{
if(root==NULL)
{
root=(node*)malloc(sizeof(node));
root->data=key;
root->left=NULL;
root->right=NULL;
return root;
}
if(key<root->data)
root->left=insert(root->left,key);
else if(key>root->data)

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 3


Data Structures and Applications (CS204) Module 4

root->right=insert(root->right,key);
return root;
}
Deletion Operation in BST
Deleting a node from Binary search tree has following three cases...
Case 1: Deleting a Leaf node (A node with no
children)Case 2: Deleting a node with one child
Case 3: Deleting a node with two children

Case 1: Deleting a leaf node


We use the following steps to delete a leaf node from
BST... Step 1: Find the node to be deleted using search
operation
Step 2: Delete the node using free function (If it is a leaf) and terminate the function.

Case 2: Deleting a node with one child


We use the following steps to delete a node with one child from
BST...Step 1: Find the node to be deleted using search operation
Step 2: If it has only one child, then create a link between its parent and child
nodes.Step 3: Delete the node using free function and terminate the function.

Case 3: Deleting a node with two children


We use the following steps to delete a node with two children from
BST...Step 1: Find the node to be deleted using search operation
Step 2: If it has two children, then find the largest node in its left subtree (OR) the smallest node in its
rightsubtree.
Step 3: Swap both deleting node and node which found in above step.
Step 4: Then, check whether deleting node came to case 1 or case 2 else goto
steps 2Step 5: If it comes to case 1, then delete using case 1 logic.
Step 6: If it comes to case 2, then delete using case 2 logic.
Step 7: Repeat the same process until node is deleted from the

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 4


Data Structures and Applications (CS204) Module 4

tree.
node* deletekey(node *root,int key)
{
node *temp;
if(root==NULL)
{
printf("Key not found");
return NULL;
}
if(key<root->data)
root->left=deletekey(root->left,key);
else if(key>root->data)
root->right=deletekey(root->right,key);
else
{
if(root->left==NULL && root->right==NULL)
{
free(root);
return NULL;
}
else if(root->left==NULL)
{
temp=root;
root=root->right;
free(temp);
return NULL;
}
else if(root->right==NULL)
{
temp=root;
root=root->left;
free(temp);
return NULL;
}
else
{
temp=findleft(root->right);
root->data=temp->data;
root->right=deletekey(root->right,temp->data);
}
}
return root;
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 5


Data Structures and Applications (CS204) Module 4

node* findleft(node *temp)


{
while(root->left!=NULL)
temp=temp->left;
return temp;
}
To find max and min value in BST

int minvalue(node *root)


{
node *temp=root;
while(temp->left!=NULL)
temp=temp->left;
return temp->data;
}
int maxvalue(node *root)
{
node *temp=root;
while(temp->right!=NULL)
temp=temp->right;
return temp->data;

}
Counting the number of leaf nodes in BST
int leafnodes(node *root)
{
static int count=0;
if(root!=NULL)
{
leafnodes(root->left);
if(root->left==NULL && root->right==NULL)
count++;
leafnodes(root->right);
}
return count;
}

Traversal Operation in BST


Inorder Traversal Function
If root is not NULL, recursively call inorder on the left child.
Print root->data.
Recursively call inorder on the right child.
void inorder(node *root)
{

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 6


Data Structures and Applications (CS204) Module 4

if(root!=NULL)
{
inorder(root->left);
printf("%d\t",root->data);
inorder(root->right);
}
}
Preorder Traversal Function
If root is not NULL, Print root->data.
Recursively call preorder on the left child.
Recursively call preorder on the right child.
void preorder(node *root)
{
if(root!=NULL)
{
printf("%d\t",root->data);
preorder(root->left);
preorder(root->right);
}
}

Postorder Traversal Function


If root is not NULL, recursively call postorder on the left child.
Recursively call postorder on the right child.
Print root->data.
void postorder(node *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d\t",root->data);
}
}
Lab program -6
Title: Design, Develop and Implement a menu driven Program in C for traversing a tree and search a
given item.
Program:

#include<stdio.h>
#include<stdlib.h>

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 7


Data Structures and Applications (CS204) Module 4

struct BST
{
int data;
struct BST *left;
struct BST *right;
};
typedef struct BST node;
node* insert(node *root,int key)
{
if(root==NULL)
{
root=(node*)malloc(sizeof(node));
root->data=key;
root->left=NULL;
root->right=NULL;
return root;
}
if(key<root->data)
root->left=insert(root->left,key);
else if(key>root->data)
root->right=insert(root->right,key);
return root;
}
void inorder(node *root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%d\t",root->data);
inorder(root->right);
}
}
void preorder(node *root)
{
if(root!=NULL)
{

printf("%d\t",root->data);
preorder(root->left);
preorder(root->right);
}
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 8


Data Structures and Applications (CS204) Module 4

void postorder(node *root)


{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d\t",root->data);
}
}
void search(node *root, int key)
{
if (root == NULL)
{
printf("key not found\n");
return;
}
if(root->data == key)
{
printf("key found\n");
return;
}
if (key<root->data)
search(root->left, key);
else
search(root->right, key);
}
int main()
{
int n,i,key,choice;
node *root=NULL,*temp;
printf("enter the number of nodes\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the element\n");
scanf("%d",&key);
root=insert(root,key);
}
while(1)
{
printf("enter 1 for inorder\n 2 for preorder\n 3 for postorder\n 4 for search\n 5 for exit\n");
scanf("%d",&choice);

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 9


Data Structures and Applications (CS204) Module 4

switch(choice)
{
case 1: printf("\ninorder is \n");
inorder(root);
break;
case 2: printf("\npre order is \n");
preorder(root);
break;
case 3: printf("\npost order is \n");
postorder(root);
break;
case 4:printf("\nenter the element to be searched");
scanf("%d",&key);
search(root,key);
break;
case 5: exit(0);
default:printf("invalid choice\n");
}
}
}
4.7 Threaded Binary Tree
Two many null pointers are there in current representation of binary trees
n: number of nodes
number of non-null links (edges): n-1
total links: 2n
null links: 2n-(n-1)=n+1
Replace these null pointers with some useful “threads”.

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 10


Data Structures and Applications (CS204) Module 4

To construct the threads we use the following rules (assume that ptr represents a node) :
If ptr->left_child is null,
replace it with a pointer to the node that would be
visited before ptr in an inorder traversal
If ptr->right_child is null,
replace it with a pointer to the node that would be
visited after ptr in an inorder traversal

In the above figure two threads have been left dangling: one is the left child of H, the other is the right
child of G. In order that we leave no loose threads, we will assume a header node for all threaded binary
trees.
struct threadednode
{
int data;
struct threadednode *left;
struct threadednode *right;
int lthread;
int rthread;
};
Empty Threaded Binary Tree

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 11


Data Structures and Applications (CS204) Module 4

Notice that we have handled the problem of the loose threads by having them point to the head node,
root.
Types of Threaded Binary Tree
There are two types of threaded Binary Tree:

1) One-way threaded Binary Tree 2) Two-way threaded Binary Tree


One-way threaded Binary trees:

In one-way threaded binary trees, a thread will appear either in the right or left link field of a node. If it
appears in the right link field of a node then it will point to the next node that will appear on performing
in order traversal. Such trees are called Right threaded binary trees. If thread appears in the left field of
a node then it will point to the nodes inorder predecessor. Such trees are called Left threaded binary
trees. Left threaded binary trees are used less often as they don't yield the last advantages of right
threaded binary trees. In one-way threaded binary trees, the right link field of last node and left link field
of first node contains a NULL. In order to distinguish threads from normal links they are represented by
dotted lines.

Two-way threaded Binary Trees:

In two-way threaded Binary trees, the right link field of a node containing NULL values is replaced by a
thread that points to nodes inorder successor and left field of a node containing NULL values is replaced
by a thread that points to nodes inorder predecessor.

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 12

You might also like