Write a C program which uses Binary search tree library and implements two more functions.

Data Structure / Assignment 7 / Set B

Binary Search Tree

#include<stdio.h>
#include<stdlib.h>
#include "btree.h"
int main()
{
int ch,n,i,value,cnt;
struct bst *newnode,*root,*temp;
root=NULL;
while(1)
{
printf("\n---Binary Search Tree---\n");
printf("1.Insert\n");
printf("2.Search\n");
printf("3.Count Total Nodes\n");
printf("4.Count Leaf Nodes\n");
printf("5.Count Non Leaf Nodes\n");
printf("6.Exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: 	printf("\nHow many nodes to create:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
newnode=create();
printf("\nEnter the node data:");
scanf("%d",&newnode->data);
if(root==NULL)
root=newnode;
else
insert(root,newnode);
}
break;
case 2: 	printf("\nEnter the node value to be searched:");
scanf("%d",&value);
temp=search(root,value);
if(temp==NULL)
printf("\nNode Not Found\n");
else
printf("\nNode Found\n");
break;
case 3:	cnt=count(root);
printf("\nTotal Nodes=%d\n",cnt);
break;
case 4:	cnt=countleaf(root);
printf("\nTotal Leaf Nodes=%d\n",cnt);
break;
case 5:	cnt=countnleaf(root);
printf("\nTotal Non Leaf Nodes=%d\n",cnt);
break;
case 6: exit(0);
default: printf("\nInvalid Choice\n");
}//switch
}//while
}//main

Btree.h

#include<stdio.h>
#include<stdlib.h>
struct bst
{
int data;
struct bst *lchild,*rchild;
}node;
int cnt=0,leafcnt=0,nleafcnt=0;
//creating a new node
struct bst *create()
{
struct bst *temp=(struct bst*)malloc(sizeof(struct bst));
temp->lchild=NULL;
temp->rchild=NULL;
return temp;
}
//inserting a above created node at proper position
void insert(struct bst *r, struct bst *new1)
{
//if new node is less than parent node
if(new1->data < r->data)
{
if(r->lchild==NULL)
r->lchild=new1;
else
insert(r->lchild,new1);
}
//if new node is greater than parent node
if(new1->data > r->data)
{
if(r->rchild==NULL)
r->rchild=new1;
else
insert(r->rchild,new1);
}
}
//to search an element in Binary Search Tree
struct bst *search(struct bst *r, int key)
{
struct bst *temp;
temp=r;
while(temp!=NULL)
{
if(temp->data==key)
return temp;
if(key < temp->data)
temp=temp->lchild;
else
temp=temp->rchild;
}
return NULL;
}
//to count total number of nodes
int count(struct bst *temp)
{
if(temp!=NULL)
{
cnt++;
count(temp->lchild);
count(temp->rchild);
}
return cnt;
}
//to count total number of leaf nodes
int countleaf(struct bst *temp)
{
if(temp!=NULL)
{
if(temp->lchild==NULL && temp->rchild==NULL)
leafcnt++;
countleaf(temp->lchild);
countleaf(temp->rchild);
}
return leafcnt;
}
//to count total number of non leaf nodes
int countnleaf(struct bst *temp)
{
if(temp!=NULL)
{
if(temp->lchild!=NULL || temp->rchild!=NULL)
nleafcnt++;
countnleaf(temp->lchild);
countnleaf(temp->rchild);
}
return nleafcnt;
}

The above file is header file btree.h

Output:
---Binary Search Tree---
1.Insert
2.Search
3.Count Total Nodes
4.Count Leaf Nodes
5.Count Non Leaf Nodes
6.Exit
Enter your choice:1
How many nodes to create:3
Enter the node data:2
Enter the node data:3
Enter the node data:4
---Binary Search Tree---
1.Insert
2.Search
3.Count Total Nodes
4.Count Leaf Nodes
5.Count Non Leaf Nodes
6.Exit
Enter your choice:2
Enter the node value to be searched:3
Node Found
---Binary Search Tree---
1.Insert
2.Search
3.Count Total Nodes
4.Count Leaf Nodes
5.Count Non Leaf Nodes
6.Exit
Enter your choice:3
Total Nodes=3
---Binary Search Tree---
1.Insert
2.Search
3.Count Total Nodes
4.Count Leaf Nodes
5.Count Non Leaf Nodes
6.Exit
Enter your choice:6

By Admin

4 thoughts on “a) Write a C program which uses Binary search tree library and implements two more functions.<br>Create(T, n) – inserts n nodes in the Binary search tree where the values are either accepted from user or randomly generated (use insert) <br>Count(T) – returns the number of nodes in the tree”
  1. Hi there, this weekend is fastidious designed for
    me, for the reason that this point in time i am reading this great informative post here at my house.

  2. What i don’t understood is actually how you are not really a lot more well-appreciated than you may be now.

    You’re very intelligent. You realize thus considerably on the subject of this subject, made me individually consider it from
    numerous numerous angles. Its like men and women don’t seem to
    be fascinated except it is something to do with Woman gaga!
    Your personal stuffs great. All the time take care of it up!

  3. I think this is among the most important info for me.
    And i’m glad reading your article. But want to remark on some general things, The website style is perfect, the articles is really
    great : D. Good job, cheers

Leave a Reply

Your email address will not be published. Required fields are marked *