Trees
Trees
A binary tree which has the property that all elements in the left subtree of a node ‘n’
are less than the contents of ‘n’, and all elements in the right subtree of ‘n’ are greater
than or equal to the contents of n. A binary tree that has this property is called a binary
search tree.
If a binary search tree is traversed in inorder (left, root, right) and the contents of each
node are printed as the node is visited, then the numbers are printed in ascending
order.
14,15,4,9,7,18,3,5,16,4,20,17,9,14,5
1
Binary Search tree
2
Function to insert an item into binary search tree ( avoid duplicate elements )
4
Write a C program to implement a binary search tree of integers and perform the
following traversal techniques:
i) In-order traversal
ii )Find the maximum element
iii) Search an element
5
Write a C function to search an element in a binary search tree.
void search ( NODE root, int item)
{
if(root==NULL)
return root;
while(root!=NULL)
{ if(rootinfo==item)
break;
if(item<rootinfo)
root=rootllink;
else
root=rootrlink;
}
if(root==NULL)
{
printf(“Element is not found or unsuccessful search”);
return root;
}
else
printf(“Successful search”);
return root;
}
6
Write a C function to find maximum value in a Binary search
tree:
while(currlink!=NULL)
{
cur=currlink;
}
printf(“Maximum element=%d”, curinfo);
}
}
7
Write a C function to count the number of nodes in BST
int count_Node(Node root)
{
if(root!=NULL)
{
count_Node(rootleft);
count++;
count_Node(rootright);
return count;
}
Write a C function to count the number of leaf nodes:
int count_Leaf(Node root)
{
if(root!=NULL)
{
count_Leaf(rootleft);
if((rootleft==NULL) && (rootright==NULL))
count++;
count_Leaf(rootright);
}
return count;
8
}