-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.cpp
More file actions
279 lines (201 loc) · 5.98 KB
/
BinarySearchTree.cpp
File metadata and controls
279 lines (201 loc) · 5.98 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <iostream>
#include <cstdlib>
#include <fstream>
#include "NodeTypes.h"
#include "BinaryTree.h"
#include "BinarySearchTree.h"
#include "Data.h"
using namespace std;
BTNode * initBSTFromFile (char fileName[]) {
BTNode * root = NULL;
ifstream inputFile;
Movie movie;
int numMovies;
inputFile.open(fileName);
if (!inputFile.is_open()) {
cout << "Input file " << fileName << " could not be opened. Aborting ..." << endl;
return root;
}
numMovies = 0;
inputFile >> movie.ID;
cout << "Root of BST created with " << movie.ID << endl;
while (movie.ID != "END") {
inputFile >> movie.yearReleased >> movie.duration >> movie.genre >> ws;
getline (inputFile, movie.title);
numMovies++;
root = insertBST (root, movie);
inputFile >> movie.ID;
}
inputFile.close();
cout << "-->" << numMovies << " movies read from " << fileName << " and stored in BST." << endl << endl;
return root;
}
BTNode* insertBST(BTNode* root, Movie data) {
BTNode* newNode;
if (root == NULL) {
// tree is empty
newNode = createBTNode(data);
return newNode;
}
if (data.ID < root->data.ID) {
//go to left subtree
BTNode* inserted = insertBST(root->left, data);
root->left = inserted;
inserted->parent = root;
}
else {
// go to right subtree
BTNode* inserted = insertBST(root->right, data);
root->right = inserted;
inserted->parent = root;
}
return root;
}
BTNode * containsBST (BTNode * root, string key) {
if(root==NULL){
return NULL;
}
if(root->data.ID==key){
return root;
}
if(key<root->data.ID){
//search left subtree
return (containsBST(root->left, key));
}
else{
//search right subtree
return (containsBST(root->right, key));
}
}
BTNode * minimumBST (BTNode * root) {
if(root==NULL){
return NULL;
}
//find leftmost, leftmost=smallest
if(root->left==NULL){
return root;
}
return minimumBST(root->left);
}
BTNode * maximumBST (BTNode * root) {
if(root==NULL){
return NULL;
}
//find rightmost, rightmost=largest
if(root->right==NULL){
return root;
}
return maximumBST(root->right);
}
BTNode * inOrderSuccessor (BTNode * node) {
if(node==NULL){
return NULL;
}
if(node->right!=NULL){
return minimumBST(node->right);
}
BTNode * parent;
parent=node->parent;
while(parent!=NULL && node==parent->right){
node=parent;
parent=parent->parent;
}
return parent;
}
void rangeBST (BTNode * root, string lowerID, string upperID) {
if (root==NULL){
return;
}
//if the root data is larger than the lowerID, search the left subtree (smaller keys here)
if(root->data.ID > lowerID){
rangeBST(root->left, lowerID, upperID);
}
//if the root data is within the given range, output
if (root->data.ID>=lowerID && root->data.ID<=upperID){
cout << root->data.ID << " ";
}
//if the root data is smaller than the upperID, search the right subtree(larger keys here)
if(root->data.ID < upperID){
rangeBST(root->right, lowerID, upperID);
}
}
void statisticsBST (BTNode * root) {
int heightTree = height(root);
int numTerminalOneChild = numOneChild(root);
BTNode * minimumKey = minimumBST(root);
BTNode * maximumKey = maximumBST(root);
//displaying the required values after calling the respective functions
cout << "Statistics on BST: " << endl;
cout << "Height of binary tree " << heightTree << endl;
cout << "Number of non-terminal nodes one child: " << numTerminalOneChild << endl;
cout << "Smallest key: " << minimumKey->data.ID << endl;
cout << "Biggest key: " << maximumKey->data.ID << endl;
cout << endl;
}
void inOrderTraversal(BTNode* root, Queue* q) {
//this function traverses the BST inOrder and enqueues the keys in the queue
if (!root) {
return;
}
inOrderTraversal(root->left, q);
enqueue(q, root);
inOrderTraversal(root->right, q);
}
bool isIsomorphic(BTNode* root1, BTNode* root2) {
//storing both bsts in separate queues (using the inorder logic) with the helper
//function above then dequeueing and comparing values
Queue* q1 = initQueue(100);
Queue* q2 = initQueue(100);
inOrderTraversal(root1, q1);
inOrderTraversal(root2, q2);
while (!isEmptyQueue(q1) && !isEmptyQueue(q2)) {
BTNode* node1 = dequeue(q1);
BTNode* node2 = dequeue(q2);
if (node1->data.ID != node2->data.ID) {
return false;
}
}
return isEmptyQueue(q1) && isEmptyQueue(q2);
}
BTNode * deleteBST(BTNode * root, string key) {
BTNode* toDelete = containsBST(root, key);
if (toDelete == NULL) {
return root;
}
if (root==NULL){
return root; //if the BST contains the key, assign the node to a BTNode
}
if (key <root->data.ID){ //searching left subtree if the key is smaller than the root data
root->left=deleteBST(root->left, key);
}
else if(key > root->data.ID){ //searching right subtree if the key is larger than the root data
root->right=deleteBST(root->right, key);
}
// Current root is the node to be deleted
else{
if(root->left == NULL && root->right==NULL){ // leaf
delete root;
return NULL;
}
else if(root->left==NULL){ // one right child
BTNode * node = root->right;
delete root;
return node;
}
else if(root->right==NULL){ // one left child
BTNode * node = root->left;
delete root;
return node;
}
//two children
else{
// find the in-order successor
BTNode * successor = inOrderSuccessor(root);
// replace the current root's data with the inOrder successor's data
root->data = successor ->data;
// delete the in-order successor from the right subtree
root->right = deleteBST(root->right, successor->data.ID);
}
}
return root;
}