-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBinarySearchTree.c
127 lines (124 loc) · 3.05 KB
/
BinarySearchTree.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
#include<stdio.h>
#include<stdlib.h>
struct bstree{
int data;
struct bstree *left,*right;
};
struct bstree *getnode();
struct bstree *insert(struct bstree *,struct bstree *);
struct bstree *delete(struct bstree *,int);
void inorder(struct bstree *);
void preorder(struct bstree *);
void postorder(struct bstree *);
struct bstree *minvalue(struct bstree *); //for inorder successor to use in delete
int main()
{
struct bstree *root=NULL,*newnode;
int ch,num,key;
while(1)
{
printf("\nENTER CHOICE\n");
printf(" 1:INSERT\n 2:DELETE\n 3:INORDER\n PREORDER\n POSTORDER\n 4:EXIT\n");
scanf("%d",&ch);
switch(ch)
{
case 1: newnode=getnode();
printf("ENTER THE NUMBER TO INSERT\n");
scanf("%d",&num);
newnode->data=num;
root=insert(root,newnode);
break;
case 2:printf("ENTER THE NUMBER TO DELETE\n");
scanf("%d",&key);
root=delete(root,key);
break;
case 3:printf("INORDER TRAVERSAL IS\n");
inorder(root);
printf("\nPREORDER TRAVERSAL IS\n");
preorder(root);
printf("\nPOSTORDER TRAVERSAL IS\n");
postorder(root);
break;
case 5:exit(0);
}
}
return 0;
}
struct bstree *getnode()
{
struct bstree *newnode;
newnode=(struct bstree *)malloc(sizeof(struct bstree));
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
struct bstree *insert(struct bstree *root,struct bstree *newnode)
{
if(root==NULL)
{return newnode;}
if(newnode->data < root->data)
root->left=insert(root->left,newnode);
else // if (newnode->data>root->data) we can use this also
root->right=insert(root->right,newnode);
}
void inorder(struct bstree *root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%d\t",root->data);
inorder(root->right);
}
}
void preorder(struct bstree *root)
{
if(root!=NULL)
{
printf("%d\t",root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct bstree *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d\t",root->data);
}
}
struct bstree *delete(struct bstree *root,int key)
{
if (root ==NULL)
return root;
if (key<root->data)
{root->left=delete(root->left,key);}
else if(key>root->data)
{root->right=delete(root->right,key);}
else
{
if(root->left==NULL)//if left child doesn't exist
{struct bstree *temp=root->right;//copy right child
free(root);//delete that root
return temp;
}
else if(root->right==NULL)//if right child doesn't exist
{
struct bstree *temp=root->left;//copy left child
free(root);//delete that root
return temp;
}
struct bstree *temp=minvalue(root->right);
root->data=temp->data;// to replace root by inorder successor and then delete
root->right=delete(root->right,temp->data);
}
return root;
}
struct bstree *minvalue(struct bstree *root)
{
struct bstree *cur=root;
while(cur->left!=NULL)
{cur=cur->left;}
return cur;
}