-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbst-array.cpp
More file actions
232 lines (208 loc) · 4.88 KB
/
bst-array.cpp
File metadata and controls
232 lines (208 loc) · 4.88 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Implementation of binary search tree and its functions
#include <iostream>
#include <cstdio>
using namespace std;
class node {
public:
int data;
node* left;
node* right;
node(int data) {
this->data = data;
left = right = NULL;
}
};
class BST {
public:
node* root;
int size;
BST(node* root, int size) {
this->root = root;
this->size = size;
}
};
node* treeSearchRecursive(BST* T, int key);
node* treeSearchIterative(BST* T, int key);
node* treeMin(BST* T);
node* treeMax(BST* T);
node* treeSuccessor(BST* T, node* x);
void treeInsert(BST* T, int key);
void treeDelete(BST* T, int key);
void inorder(BST* T);
node* treeSearchRecursive(BST* T, int key) {
node* root = T->root;
if (!root || root->data == key)
return root;
if (key < root->data)
return treeSearchRecursive(new BST(root->left, T->size), key);
else return treeSearchRecursive(new BST(root->right, T->size), key);
}
node* treeSearchIterative(BST* T, int key) {
node* root = T->root;
while (root && root->data != key) {
if (key < root->data)
root = root->left;
else root = root->right;
}
return root;
}
node* treeMin(BST* T) {
node* root = T->root;
while (root->left)
root = root->left;
return root;
}
node* treeMax(BST* T) {
node* root = T->root;
while (root->right)
root = root->right;
return root;
}
node* treeSuccessor(BST* T, node* x) {
node* root = T->root;
if (x->right)
return treeMin(new BST(root->right, T->size));
node* succ = NULL;
while (root) {
if (x->data < root->data) {
succ = root;
root = root->left;
}
else root = root->right;
}
return succ;
}
void treeInsert(BST* T, int key) {
node* root = T->root;
if (!root) {
T->root = new node(key);
++(T->size);
return;
}
while (root->left || root->right) {
if (key <= root->data)
root = root->left;
else root = root->right;
}
if (key <= root->data)
root->left = new node(key);
else root->right = new node(key);
++(T->size);
return;
}
void treeDelete(BST* T, int key) {
node* root = T->root;
if (!root)
return;
node* ptr = root;
node* p;
node* parent = NULL;
while (ptr && ptr->data != key) {
parent = ptr;
if (key <= ptr->data)
ptr = ptr->left;
else ptr = ptr->right;
}
// checking if key exists in BST
if (!ptr) {
cout<<"Element doesnt exist in BST.."<<endl;
return;
}
p = ptr;
if (!p->left) {
if (!parent)
T->root = p->right;
else if (p == parent->left)
parent->left = p->right;
else parent->right = p->right;
}
else if (!p->right) {
if (!parent)
T->root = p->left;
else if (p == parent->left)
parent->left = p->left;
else parent->right = p->left;
}
else {
node* succ = p->right;
node* succ_parent = p;
while (succ->left) {
succ_parent = succ;
succ = succ->left;
}
if (!parent)
T->root = succ;
else if (p == parent->left)
parent->left = succ;
else parent->right = succ;
if (succ == succ_parent->left)
succ_parent->left = succ->right;
else succ_parent->right = succ->right;
succ->left = p->left;
succ->right = p->right;
}
--(T->size);
return;
}
void inorder(BST* T) {
node* root = T->root;
if (root) {
inorder(new BST(root->left, T->size));
cout<<root->data<<" ";
inorder(new BST(root->right, T->size));
}
return;
}
int main() {
freopen("input.txt","r",stdin);
// hard-coding a BST
node* root = new node(15);
root->left = new node(6);
root->right = new node(18);
root->left->left = new node(3);
root->left->right = new node(7);
root->right->left = new node(17);
root->right->right = new node(20);
root->left->left->left = new node(2);
root->left->left->right = new node(4);
root->left->right->right = new node(13);
root->left->right->right->left = new node(9);
BST* T = new BST(root, 11);
node* result;
cout<<"Inorder traversal of BST : ";
inorder(T);
cout<<endl<<"BST recursive search (20) : ";
result = treeSearchRecursive(T, 20);
if (result)
cout<<"Element found.."<<endl;
else cout<<"Element not found.."<<endl;
cout<<"BST iterative search (100) : ";
result = treeSearchIterative(T, 100);
if (result)
cout<<"Element found.."<<endl;
else cout<<"Element not found.."<<endl;
cout<<"BST min element : ";
result = treeMin(T);
cout<<result->data<<endl;
cout<<"BST max element : ";
result = treeMax(T);
cout<<result->data<<endl;
cout<<"BST successor of 13 : ";
result = treeSuccessor(T, T->root->left->right->right);
if (result)
cout<<result->data<<endl;
else cout<<"No successor.."<<endl;
cout<<endl<<"Old size of tree: "<<T->size<<endl;
cout<<"Inserting element 1, 8 and 21 in BST : ";
treeInsert(T, 1);
treeInsert(T, 8);
treeInsert(T, 21);
inorder(T);
cout<<endl<<"New size of tree: "<<T->size<<endl;
cout<<endl<<"Deleting element 15 in BST : ";
treeDelete(T, 15);
inorder(T);
cout<<endl<<"Size of tree after deleting: "<<T->size<<endl;
cout<<endl;
return 0;
}