-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST.c
115 lines (107 loc) · 2.79 KB
/
BST.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
#include<stdio.h>
#include<stdlib.h>
struct BSTNode{
int data ;
struct BSTNode *left;
struct BSTNode *right;
};
struct BSTNode *START = NULL;
void Insert(struct BSTNode *root,int info){
struct BSTNode *n = malloc(sizeof(struct BSTNode));
n->left = NULL ;
n->right = NULL ;
n->data = info ;
if(root==NULL){
START = n ;
}
else{
while(root != NULL){
if(root->data > info ){
if(root->left == NULL){
root->left = n ;
root = n->left;
}
else
root = root->left;
}
else if(root->data< info){
if(root->right == NULL){
root->right = n;
root = n->right;
}
else
root = root->right;
}
}
}
}
struct BSTNode* Findmax(struct BSTNode *root){
if(root->right == NULL)
return(root);
else if(root->right != NULL)
return(Findmax(root->right));
}
struct BSTNode* Delete(struct BSTNode *root,int info){
struct BSTNode *temp;
if(root == NULL)
printf("\nNo such element exist");
else if(info < root->data)
root->left = Delete(root->left,info);
else if(info > root->data)
root->right =Delete(root->right,info);
else{// element found
if(root->left && root->right){
temp = Findmax(root->left);
root->data = temp->data;
root->left = Delete(root->left,root->data);
}
else{
//one or none child
temp = root ;
if(root->left == NULL)
root = root -> right ;
else if(root->right == NULL)
root = root->left;
free(temp);
}
}
return(root);
}
void Traverse(struct BSTNode *root){ //Preorder traversing
if(root){
printf(" %d ",root->data);
Traverse(root->left);
Traverse(root->right);
}
}
int menu(){
int ch ;
printf("\n1.Add value to the Binary Tree");
printf("\n2.view Tree");
printf("\n3.Delete item");
printf("\nenter your choice");
scanf("%d",&ch);
return (ch);
}
void main(){
int data;
while(1){
switch (menu()){
case 1:
printf("\nenter a value");
scanf("%d",&data);
Insert(START,data);
break;
case 2:
Traverse(START);
break;
case 3:
printf("\nenter value to delete");
scanf("%d",&data);
START = Delete(START,data);
break;
default:
printf("\ninvalid choice");
}
}
}