i II Ds Unit v Final
i II Ds Unit v Final
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
Algorithm:
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;
Procedure:
void insert(int item)
{ struct node *tmp,*parent,*location;
search(item,&parent,&location);
if(location!=NULL)
if(parent==NULL)
root=tmp;
else
if(item<parent->data)
parent->lchild=tmp;
else
parent->rchild=tmp;
}
The delete function deletes a node from the binary search tree which should not
violate properties of the binary search tree.
Algorithm:
find(item,&parent,&location);
if(location==NULL)
{
printf("Item not present in tree");
return 0;
}
Case 1: Deleting a Node that has No Children: Deletion of a leaf node means simply
removing node without any issue.
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; }
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;
Procedure:
void case_c(struct node *par,struct node *loc)
{
struct node *ptr,*ptrsave,*suc,*parsuc;
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.
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).
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
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
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.
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:
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
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.