Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

File: /home/onworks/yashab/prac7.

c Page 1 of 6

/*
FILENAME:-EXPT7.c
AUTHOR :-Yash Bhageriya
DATE :-23-09-2019
AIM :-To study and implement Consider a binary search tree node-
treeNode,representing a self referential structure.
Write a menu driven program to-(1)insert a node into BST,(2)delete
a node from BST,(3)print traversals(inorder,preorder,postorder)for a BST,
(4)find height of BST,(5)count and print all leaf nodes,parent nodes.
Your program must also allow for creating a pre-allocated list
before the start of BST ADT.You must not use global variables.
*/
#include<stdio.h>
10 #include<stdlib.h>
/*TREE NODE*/
struct treenode{
int data;
struct treenode *lchild;
struct treenode *rchild;
};
typedef struct treenode * tree;
/*Function Prototypes*/
tree CreateTreenode(void);
20 tree InsertBSTnode(tree,int);
void inorderBST(tree);
void preorderBST(tree);
void postorderBST(tree);
int heightBST(tree);
int max(int,int);
tree DeleteBSTnode(tree,int);
tree findmidnode(tree);
int ParentBSTnode(tree);
int AllNodes_BST(tree);
30 int Leaves_BST(tree);
tree emptyBST(tree);
/*MAIN FUNCTION*/
int main(){
tree root=NULL; /*INTIALIZE TREE*/
int knt,key,choice;
printf("HOW MANY NODES? ");
scanf("%d",&knt);
while(knt){
printf("\nNODE DATA?");
40 scanf("%d",&key);
root=InsertBSTnode(root,key);
knt--;
}
do{
printf("\nOperations:
\n1.INSERT\n2.TRAVERSE\n3.DELETE\n4.HEIGHT\n5.PARENT\n6.ALL NODES\n7.LEAVE
NODES\n8.EMPTY\n0.EXIT\n");
scanf("%d",&choice);
switch(choice){
case 0:
break;
50 case 1:
printf("\nNODE DATA?");
scanf("%d",&key);
root=InsertBSTnode(root,key);
break;
case 2:
printf("\nOperations:\n1.Inorder\n2.Preorder\n3.Postorder\n");
scanf("%d",&key);
switch(key){
case 1:
60 printf("TREE INORDERED IS...");
File: /home/onworks/yashab/prac7.c Page 2 of 6

inorderBST(root);
break;
case 2:
printf("\nTREE PREORDERD IS...");
preorderBST(root);
break;
case 3:
printf("\nTREE POST ORDERED IS...");
postorderBST(root);
70 break;
}
break;
case 3:
printf("Enter Key To Delete:\n");
scanf("%d",&key);
root=DeleteBSTnode(root,key);
inorderBST(root);
break;
case 4:
80 printf("\nHEIGHT IS... %d",heightBST(root));
break;
case 5:
printf("\nParent Nodes is :\n");
key=ParentBSTnode(root)-1;
printf("Number of Parents are %d\n",key);
break;
case 6:
knt=AllNodes_BST(root);
printf("\nNumber of total nodes in a BST are %d",knt);
90 break;
case 7:
knt=Leaves_BST(root);
printf("\nNumber of leaves nodes in a BST are %d",knt);
break;
case 8:
root=emptyBST(root);
break;
default:
printf("INVALID\n");
100 }
}while(choice!=0);
return 0;
}
/**Create Tree Node**/
tree CreateTreenode(void){
tree neww;
neww=(tree) malloc(sizeof(struct treenode));//return(tree)
malloc(sizeof(struct treenode))//
return neww;
}
110 /**InsertBSTnode**/
tree InsertBSTnode(tree root,int key){
if (root == NULL){
root=CreateTreenode();
(root->data)=key;
(root->lchild)=NULL;
(root->rchild)=NULL;
}
else if(key<root->data)
(root->lchild)=InsertBSTnode(root->lchild,key);
120 else if(key>root->data)
(root->rchild)=InsertBSTnode(root->rchild,key);
else
printf("Duplicate Key...Not Inserted.../n");
return root;
}
File: /home/onworks/yashab/prac7.c Page 3 of 6

/**Inorder**/
void inorderBST(tree root){
if(root!=NULL){
inorderBST(root->lchild);
130 printf("%d\t",(root->data));
inorderBST(root->rchild);
}
}
/**Preorder**/
void preorderBST(tree root){
if(root!=NULL){
printf("%d\t",(root->data));
preorderBST(root->lchild);
preorderBST(root->rchild);
140 }
}
/**Postorder**/
void postorderBST(tree root){
if(root!=NULL){
postorderBST(root->lchild);
postorderBST(root->rchild);
printf("%d\t",(root->data));
}
}
150 /**Height OF Tree**/
int heightBST(tree root){
if(root==NULL)
return 0;
if(root->lchild==NULL && root->rchild==NULL)
return 1;
return max(heightBST(root->lchild),heightBST(root->rchild))+1;
}
int max(int lheight,int rheight){
return(lheight>=rheight ? lheight:rheight);
160 }
/**Delete Node**/
tree DeleteBSTnode(tree root,int key){
if (root == NULL){
printf("Delete Failed...\n");
return NULL;
}
tree temp;
if(key<(root->data))
(root->lchild)=DeleteBSTnode((root->lchild),key);
170 else if(key>root->data)
(root->rchild)=DeleteBSTnode(root->rchild,key);
else{
if((root->lchild)!=NULL && (root->rchild)!=NULL){
temp=findmidnode(root->rchild);
(root->data)=(temp->data);
(root->rchild)=DeleteBSTnode((root->rchild),(root->data));
}
else{
temp=root;
180 if((root->lchild)==NULL)
root=(root->rchild);
else if(root->rchild==NULL)
root=(root->lchild);
}
}
return (root);
}
/**FIND MIDDLE NODE**/
tree findmidnode(tree root){
190 if(root==NULL)
return NULL;
File: /home/onworks/yashab/prac7.c Page 4 of 6

if((root->lchild)==NULL)
return (root);
return(findmidnode(root->lchild));
}
/**Parent BST Node**/
int ParentBSTnode(tree root){
if(root==NULL ||(root->lchild==NULL && root->rchild==NULL))
return 0;
200 printf("%d\t",root->data);
return(ParentBSTnode(root->lchild)+ParentBSTnode(root->rchild)+1);
}
/**ALL NODES**/
int AllNodes_BST(tree root){
int knt=1;
if(root==NULL)
return 0;
printf("%d\t",root->data);
knt=knt+AllNodes_BST(root->lchild)+AllNodes_BST(root->rchild);
210 return knt;
}
/**LEAVE BST**/
int Leaves_BST(tree root){
if(root==NULL)
return 0;
if(root->lchild==NULL && root->rchild==NULL){
printf("%d",root->data);
return 1;
}
220 return(Leaves_BST(root->lchild)+Leaves_BST(root->rchild));
}
/**EMPTY BST NODE**/
tree emptyBST(tree root){
if(root!=NULL){
emptyBST(root->lchild);
emptyBST(root->rchild);
printf("Release node with key %d\n",(root->data));
free(root);
}
230 }
/**
HOW MANY NODES? 16
NODE DATA?50
NODE DATA?40
NODE DATA?30
NODE DATA?25
NODE DATA?20
NODE DATA?27
NODE DATA?55
240 NODE DATA?52
NODE DATA?60
NODE DATA?70
NODE DATA?65
NODE DATA?90
NODE DATA?56
NODE DATA?58
NODE DATA?57
NODE DATA?59
Operations:
250 1.INSERT
2.TRAVERSE
3.DELETE
4.HEIGHT
5.PARENT
6.ALL NODES
7.LEAVE NODES
8.EMPTY
File: /home/onworks/yashab/prac7.c Page 5 of 6

0.EXIT
2
260 Operations:
1.Inorder
2.Preorder
3.Postorder
2
TREE PREORDERD IS...50 40 30 25 20 27 55
52 60 56 58 57 59
70 65 90
Operations:
1.INSERT
2.TRAVERSE
270 3.DELETE
4.HEIGHT
5.PARENT
6.ALL NODES
7.LEAVE NODES
8.EMPTY
0.EXIT
3
Enter Key To Delete:
30
280 20 25 27 40 50 52 55 56 57
58 59 60 65 70 90
Operations:
1.INSERT
2.TRAVERSE
3.DELETE
4.HEIGHT
5.PARENT
6.ALL NODES
7.LEAVE NODES
8.EMPTY
290 0.EXIT
3
Enter Key To Delete:
55
20 25 27 40 50 52 56 57 58
59 60 65 70 90
Operations:
1.INSERT
2.TRAVERSE
3.DELETE
4.HEIGHT
300 5.PARENT
6.ALL NODES
7.LEAVE NODES
8.EMPTY
0.EXIT
4
HEIGHT IS... 5
Operations:
1.INSERT
2.TRAVERSE
310 3.DELETE
4.HEIGHT
5.PARENT
6.ALL NODES
7.LEAVE NODES
8.EMPTY
0.EXIT
5
Parent Nodes is :
50 40 25 56 60 58 70
320 Number of Parents are 6
File: /home/onworks/yashab/prac7.c Page 6 of 6

Operations:
1.INSERT
2.TRAVERSE
3.DELETE
4.HEIGHT
5.PARENT
6.ALL NODES
7.LEAVE NODES
8.EMPTY
330 0.EXIT
**/

You might also like