-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSTCOUNT.C
153 lines (147 loc) · 3.74 KB
/
BSTCOUNT.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
struct node* left;
int key;
struct node* right;
}nodetype;
nodetype* newNode(int item){
nodetype* temp=(nodetype*)malloc(sizeof(nodetype));
temp->key=item;
temp->left=temp->right=NULL;
return temp;
}
void inorder(nodetype* root){
if(root!=NULL){
inorder(root->left);
printf("%d\n",root->key);
inorder(root->right);
}
}
void postorder(nodetype* root){
if(root!=NULL){
inorder(root->left);
inorder(root->right);
printf("%d\n",root->key);
}
}
void preorder(nodetype* root){
if(root!=NULL){
printf("%d\n",root->key);
inorder(root->left);
inorder(root->right);
}
}
nodetype* insert(nodetype* node,int key){
if(node==NULL){
return newNode(key);
}
if(key<node->key){
node->left=insert(node->left,key);
}
else if(key>node->key){
node->right=insert(node->right,key);
}
return node;
}
nodetype* minValueNode(nodetype* node)
{
nodetype* current = node;
while (current && current->left != NULL)
current = current->left;
return current;
}
nodetype* deleteNode(nodetype* root, int key)
{
if (root == NULL)
return root;
// SEARCHING
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
// DELETION
else {
if (root->left == NULL) {
nodetype* temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL) {
nodetype* temp = root->left;
free(root);
return temp;
}
nodetype* temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
return root;
}
int count(nodetype *root)
{
if (root ==NULL)
return 0;
else
{
return 1 + count(root->left) + count(root->right);
}
}
int countLeafNode(struct node *root){
if(root == NULL)
return 0;
if(root->left == NULL && root->right == NULL)
return 1;
else
return countLeafNode(root->left) + countLeafNode(root->right);
}
int main(){
nodetype* root=NULL;
root = insert(root, 1);
// insert(root, 30);
// insert(root, 20);
// insert(root, 40);
// insert(root, 70);
// insert(root, 60);
while(1){
int key,ch;
printf("enter 1 for insertion anything 2 for inorder 3 for reorder 4 for postorder 5 for deletion other to exit");
scanf("%d",&ch);
if(ch==1){
printf("enter data\n");
scanf("%d",&key);
insert(root,key);
}
else if(ch==2){
printf("inorder display\n");
inorder(root);
}
else if(ch==3){
printf("preorder display\n");
preorder(root);
}
else if(ch==4){
printf("postorder display\n");
postorder(root);
}
else if(ch==5){
int delnode;
printf("enter node to be deleted \n");
scanf("%d",&delnode);
root=deleteNode(root,delnode);
}
else if(ch==6){
// int countnode=count(root);
printf("number of nodes %d\n",count(root));
}
else if(ch==7){
// int countnode=count(root);
printf("number of leafnodes %d\n",countLeafNode(root));
}
else{
break;
}
}
return 0;
}