This repository has been archived by the owner on May 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary_Search_Tree.cpp
216 lines (198 loc) · 4.01 KB
/
Binary_Search_Tree.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Making a Binary Search Tree of a sorted array // Also with insert and delete functionality! // Time complexity - O(h) - Height of the tree
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
void inorder(struct node* root) // Inorder Traversal
{
if(root->left!=NULL)
{
inorder(root->left);
}
printf("\nThe value is %d\n",root->data);
if(root->right!=NULL)
{
inorder(root->right);
}
}
struct node* makebst(int a[],int start, int end) // BST from a sorted array //
{
if(start == end)
{
return NULL;
}
else
{
int mid = (start + end)/2;
struct node* temp = new node;
temp->data = a[mid];
temp->left = makebst( a, start, mid);
temp->right = makebst(a,mid+1,end);
return temp;
}
}
void insertbst( struct node* root, int a) // Insertion into a Binary Search Tree in O(h) time -- Assume all elements are distinct -- h - height of the tree -- Already values exist//
{
struct node* temp = root;
int flag;
while(1)
{
if(temp->data > a)
{
flag = 0;
if(temp->left!=NULL)
{
temp=temp->left;
}
else
{
break;
}
}
else if(temp->data < a)
{
flag = 1;
if(temp->right!=NULL)
{
temp=temp->right;
}
else
{
break;
}
}
}
if(flag)
{
struct node* addnode = new node;
addnode->data = a;
addnode->left = addnode->right = NULL;
temp->right = addnode;
}
else
{
struct node* addnode = new node;
addnode->data = a;
addnode->left = addnode->right = NULL;
temp->left = addnode;
}
}
void deletenode( struct node* root, int a) // Very similar to search // Assume that there are already a few elements in the BST
{
struct node* temp = root;
struct node* prev1 = root;
int lr;
while(1)
{
if(a>temp->data)
{
prev1 = temp;
temp=temp->right;
lr = 1;
printf("\nGoing right\n");
}
else if(a<temp->data)
{
prev1 = temp;
temp=temp->left;
lr = 0;
printf("\nGoing Left\n");
}
else
{
printf("\nEntered the deletion phase\n");
if(temp->left== NULL && temp->right == NULL)
{
printf("\nEncountered case 1.....\n");
if(lr)
{
prev1->right = NULL;
}
else
{
prev1->left = NULL;
}
temp = NULL;
delete temp;
}
else if(temp->left!=NULL)
{
int flag1 = 1;
if(temp->left->left!=NULL && temp->left->right == NULL)
{
flag1 = 0;
}
if(flag1)
{
printf("\n Encountered case 2 ....\n");
int flag = 0;
struct node* temp1 = temp->left;
struct node* previous = temp;
while(temp1->right!=NULL)
{
previous = temp1;
temp1 = temp1->right;
flag = 1;
}
temp->data = temp1->data;
if(flag)
{
previous->right = NULL;
}
else
{
temp->left = NULL;
}
delete(temp1);
}
else
{
temp->data = temp->left->data;
temp->left = temp->left->left;
struct node* temp1 = temp->left;
temp1=NULL;
delete temp1;
}
}
else
{
printf("\nEncountered case 3 ....\n");
temp->data = temp->right->data;
temp->right = temp->right->right;
struct node* temp1 = temp->right;
delete(temp1);
}
break;
}
}
}
int main()
{
int n;
printf("Enter the number of elements in the array : ");
scanf("%d",&n);
int a[n];
struct node* root;
int i;
printf("\nEnter the elements in sorted order to construct BST\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
root = makebst(a,0,n);
inorder(root);
printf("\nEnter the element to be inserted. After insertion the In-Order Traversal will be printed... \n");
int elem;
scanf(" %d",&elem);
insertbst(root,elem);
inorder(root);
printf("\nEnter the element to be deleted. After deletion the In-Order Traversal will be printed... \n");
scanf(" %d",&elem);
deletenode(root,elem);
inorder(root);
return 0;
}