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

i II Ds Unit v Final

Uploaded by

pkwarrior089
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)
22 views

i II Ds Unit v Final

Uploaded by

pkwarrior089
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/ 11

UNIT -V

Binary Search Trees:


A binary search tree, also known as an ordered binary tree, is a variant of binary trees
in which the nodes are arranged in an order.

Properties:
Binary Search Tree is a node-based binary tree data structure which has the following
properties:
 The left subtree of a node contains only nodes with keys lesser than the node’s key. 
 The right subtree of a node contains only nodes with keys greater than the node’s key. 
 Nodes in binary search tree has distinct elements.

Example: Create a binary search tree using the following data elements:
45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67, 81

Operations On Binary Search Trees:

Searching for a Node in a Binary Search Tree:


The search function is used to find whether a given value is present in the tree or not.
The searching process begins at the root node. Since the nodes in a binary search tree are
ordered, the time needed to search an element in the tree is greatly reduced. The average
running time of a search operation is O(log2n).

Algorithm:

SearchElement (TREE, VAL)


Step 1: IF TREE DATA = VAL OR TREE = NULL
Return TREE
ELSE
IF VAL < TREE DATA
Return search element(TREE LEFT, VAL)

Prepared by P.Sangeeta - CSE


ELSE
Return search element(TREE RIGHT, VAL)
[END OF IF]
[END OF IF]
Step 2: END
Example: Searching a node with value 67 in Binary Search Tree

Procedure:
void search(int item,struct node **par,struct node **loc)
{
struct node *ptr,*ptrsave;
if(root==NULL) /*tree empty*/
{
*loc=NULL;
*par=NULL;
return;
}
if(item==root->data) /*item is at root*/
{
*loc=root;
*par=NULL;
printf("\n %d is found",item);
return;
}
/*Initialize ptr and ptrsave*/
if(item<root->data)
ptr=root->lchild;
else
ptr=root->rchild;
ptrsave=root;
while(ptr!=NULL)
{
if(item==ptr->data)
{ *loc=ptr;
*par=ptrsave;
printf("\n %d is found",item);
return;

Prepared by P.Sangeeta - CSE


}
ptrsave=ptr;
if(item<ptr->data)
{
ptr=ptr->lchild;
}
else
ptr=ptr->rchild;
}
*loc=NULL; /*item not found*/
printf("\n %d is not found",item);
*par=ptrsave;
}

Inserting a New Node in a Binary Search Tree:


The insert function is used to add a new node with a given value at the correct
position in the binary search tree. Adding the node at the correct position means that the new
node should not violate the properties of the binary search tree.
Algorithm:

Insert (TREE, VAL)


Step 1: IF TREE = NULL
Allocate memory for TREE
SET TREE DATA = VAL
SET TREE LEFT = TREE RIGHT = NULL
ELSE
IF VAL < TREE DATA
Insert(TREE LEFT, VAL)
ELSE
Insert(TREE RIGHT, VAL)
[END OF IF]
[END OF IF]
Step 2: END
Example: Inserting nodes with value 12 and 55 in Binary Search Tree

Procedure:
void insert(int item)
{ struct node *tmp,*parent,*location;
search(item,&parent,&location);
if(location!=NULL)

Prepared by P.Sangeeta - CSE


{
printf("Item already present");
return;
}
tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=item;
tmp->lchild=NULL;
tmp->rchild=NULL;

if(parent==NULL)
root=tmp;
else
if(item<parent->data)
parent->lchild=tmp;
else
parent->rchild=tmp;
}

Deleting a Node from a Binary Search Tree:

The delete function deletes a node from the binary search tree which should not
violate properties of the binary search tree.

Algorithm:

Delete (TREE, VAL)


Step 1: IF TREE = NULL
Write "VAL not found in the tree"
ELSE IF VAL < TREE DATA
Delete(TREE->LEFT, VAL)
ELSE IF VAL > TREE DATA
Delete(TREE RIGHT, VAL)
ELSE IF TREE LEFT AND TREE RIGHT
SET TEMP = findLargestNode(TREE LEFT)
SET TREE DATA = TEMP DATA
Delete(TREE LEFT, TEMP DATA)
ELSE
SET TEMP = TREE
IF TREE LEFT = NULL AND TREE RIGHT = NULL
SET TREE = NULL
ELSE IF TREE LEFT != NULL
SET TREE = TREE LEFT
ELSE
SET TREE = TREE RIGHT
[END OF IF]
FREE TEMP
[END OF IF]
Step 2: END

Prepared by P.Sangeeta - CSE


Procedure:
int del(int item)
{
struct node *parent,*location;
if(root==NULL)
{
printf("Tree empty");
return 0;
}

find(item,&parent,&location);
if(location==NULL)
{
printf("Item not present in tree");
return 0;
}

if(location->lchild==NULL && location->rchild==NULL)


case_a(parent,location);
if(location->lchild!=NULL && location->rchild==NULL)
case_b(parent,location);
if(location->lchild==NULL && location->rchild!=NULL)
case_b(parent,location);
if(location->lchild!=NULL && location->rchild!=NULL)
case_c(parent,location);
free(location);
}

Case 1: Deleting a Node that has No Children: Deletion of a leaf node means simply
removing node without any issue.

Example: Delete node 78 from Binary Search Tree

Procedure:
void case_a(struct node *par,struct node *loc )
{
if(par==NULL) /*item to be deleted is root node*/
root=NULL;
else
if(loc==par->lchild)
par->lchild=NULL;
else
par->rchild=NULL; }

Prepared by P.Sangeeta - CSE


Case 2: Deleting a Node with One Child:
To delete a node with one child, replace the node with its child. Now, if the node is
the left child of its parent, the node’s child becomes the left child of the node’s parent.
Correspondingly, if the node is the right child of its parent, the node’s child becomes the right
child of the node’s parent.
Example: Delete node 54 from Binary Search Tree

Procedure:
void case_b(struct node *par,struct node *loc)
{
struct node *child;

/*Initialize child*/
if(loc->lchild!=NULL) /*item to be deleted has lchild */
child=loc->lchild;
else /*item to be deleted has rchild */
child=loc->rchild;

if(par==NULL ) /*Item to be deleted is root node*/


root=child;
else
if( loc==par->lchild) /*item is lchild of its parent*/
par->lchild=child;
else /*item is rchild of its parent*/
par->rchild=child;
}
Case 3: Deleting a Node with Two Children
To delete a node with two children, replace the node’s value with its in-order
predecessor (largest value in the left sub-tree) or in-order successor (smallest value in the
right sub-tree).
Example: Delete node 56 from Binary Search Tree.
Case a:

Prepared by P.Sangeeta - CSE


Case b:

Procedure:
void case_c(struct node *par,struct node *loc)
{
struct node *ptr,*ptrsave,*suc,*parsuc;

/*Find inorder successor and its parent*/


ptrsave=loc;
ptr=loc->rchild;
while(ptr->lchild!=NULL)
{ ptrsave=ptr;
ptr=ptr->lchild; }
suc=ptr;
parsuc=ptrsave;
if(suc->lchild==NULL && suc->rchild==NULL)
case_a(parsuc,suc);
else
case_b(parsuc,suc);

if(par==NULL) /*if item to be deleted is root node */


root=suc;
else
if(loc==par->lchild)
par->lchild=suc;
else
par->rchild=suc;
suc->lchild=loc->lchild;
suc->rchild=loc->rchild;
}

Traversals:
Displaying visited nodes in Binary Search tree is categorized into three traversal methods:
1. In-order Traversal
2. Pre-order Traversal
3. Post-order Traversal

In-order Traversal:
In In-Order Traversal, visiting of nodes occur in the order of Left child node, Data
and Right child node(LDR). The inorder traversal of a binary search tree produces a
sequenced list.

Prepared by P.Sangeeta - CSE


Fig: Binary Search Tree
In-Order Traversal of above Binary Search Tree is:
12 18 20 23 35 44 52
Procedure:
void inorder(struct node *ptr)
{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("%d ",ptr->data);
inorder(ptr->rchild);
}
}

Pre-order Traversal:
In Pre-Order Traversal, visiting of nodes occur in the order of Data, Left child node,
and Right child node(DLR).
Pre-Order Traversal of above Binary Search Tree is:
23 18 12 20 44 35 52
Procedure:
int preorder(struct node *ptr)
{
if(root==NULL)
{
printf("Tree is empty");
return 0;
}
if(ptr!=NULL)
{
printf("%d ",ptr->data);
preorder(ptr->lchild);
preorder(ptr->rchild);
}
}

Post-order Traversal:
In Post-Order Traversal, visiting of nodes occur in the order of Left child node, Right
child node and Data (LRD).

Prepared by P.Sangeeta - CSE


Post-Order Traversal of above Binary Search Tree is:
12 20 18 35 52 44 23
Procedure:
void postorder(struct node *ptr)
{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
postorder(ptr->lchild);
postorder(ptr->rchild);
printf("%d ",ptr->data);
}
}

Finding the Smallest Node in a Binary Search Tree:

The very basic property of the binary search tree states that the smaller value will
occur in the left sub-tree. If the left sub-tree is NULL, then the value of the root node will be
smallest as compared to the nodes in the right sub-tree.

Algorithm:

findSmallestElement(TREE)
Step 1: IF TREE = NULL OR TREE LEFT = NULL
Return TREE
ELSE
Return findSmallestElement(TREE LEFT)
[END OF IF]
Step 2: END

Finding the Largest Node in a Binary Search Tree:

To find the node with the largest value, we find the value of the rightmost node of the
right subtree.
However, if the right sub-tree is empty, then the root node will be the largest value in the tree.

Algorithm:

findLargestElement(TREE)
Step 1: IF TREE = NULL OR TREE RIGHT = NULL
Return TREE
ELSE
Return findLargestElement(TREE RIGHT)
[END OF IF]
Step 2: END

Prepared by P.Sangeeta - CSE


Applications:

Binary Search Trees are used for a lot of applications due to its ordered structure.
 Binary Search Trees are used for indexing and multi-level indexing. 
 They are also helpful to implement various searching algorithms. 
 It is helpful in maintaining a sorted stream of data.

Threaded Binary Trees:

Thread: Threads are special pointers that replace NULL entries with a special pointer to
store the in-order predecessor or the in-order successor of the node.
Binary trees containing such threads are called threaded trees.
Consider a binary tree and its linked representation as follows:

Fig: Binary Tree Fig: LinkedList representation of Binary


Tree
A threaded binary tree may correspond to one-way threading or a two-way threading.

One-way threading: In one-way threaded tree/ single-threaded tree, a thread will appear
either in the right field or the left field of the node.
Left-threaded binary tree: In left-threaded binary tree, the thread appears in the left field,
then the left field will be made to point to the in-order predecessor of the node.
Right-threaded binary tree: In right-threaded binary tree, the thread appears in the right
field, then it will point to the in-order successor of the node. Such a one-way threaded tree is
called a right threaded
binary tree.
The in-order traversal of the above binary tree is given as 8, 4, 9, 2, 5, 1, 10, 6, 11, 3, 7, 12

Fig: Linked list representation of Binary tree with one way threading

Prepared by P.Sangeeta - CSE


Node 5 contains a NULL pointer in its RIGHT field, so it will be replaced to point to node 1,
which is its in-order successor. Similarly, the RIGHT field of node 8 will point to node 4, the
RIGHT field of node 9 will point to node 2, the RIGHT field of node 10 will point to node 6,
the RIGHT field of node 11 will point to node 3, and the RIGHT field of node 12 will contain
NULL because it has no in-order successor.

Two-way threaded tree: In a two-way threaded binary tree/double-threaded tree/ fully


threaded binary tree, threads in the left field will point to the in-order predecessor of the node,
the threads in right field will point to its successor.

Fig: Linked list representation of Binary tree with two way threading

Node 5 contains a NULL pointer in its LEFT field, so it will be replaced to point to node 2,
which is its in-order predecessor. Similarly, the LEFT field of node 8 will contain NULL
because it has no in-order predecessor, the LEFT field of node 7 will point to node 3, the
LEFT field of node 9 will point to node 4, the LEFT field of node 10 will point to node 1, the
LEFT field of node 11 will contain 6, and the LEFT field of node 12 will point to node 7.

Advantages of Threaded Binary Tree:


 It enables linear traversal of elements in the tree.
 Linear traversal eliminates the use of stacks which in turn consume a lot of memory
space and computer time.
 It enables to find the parent of a given element without explicit use of parent pointers.
 Since nodes contain pointers to in-order predecessor and successor, the threaded tree
enables forward and backward traversal of the nodes as given by in-order fashion.

Prepared by P.Sangeeta - CSE

You might also like