-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbst.cpp
More file actions
245 lines (228 loc) · 5.9 KB
/
bst.cpp
File metadata and controls
245 lines (228 loc) · 5.9 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
#include <iostream>
#include <cstdlib>
#include "bst.h"
using namespace std;
BST::BST() { root = nullptr; }
//node* is not known by cpp file, and outside definition, must explicitly tell it.
BST::node *BST::CreateLeaf(int key)
{
node *n = new node;
n->key = key;
n->left = nullptr;
n->right = nullptr;
return n;
} //end of CreateLeaf
void BST::AddLeaf(int key) { AddLeafPrivate(key, root); } //end AddLeaf
void BST::AddLeafPrivate(int key, BST::node *ptr)
{
if (root == nullptr)
{
root = CreateLeaf(key);
}
else if (key < ptr->key)
{
if (ptr->left != nullptr)
{
AddLeafPrivate(key, ptr->left);
} // recursively checking left
else
{
ptr->left = CreateLeaf(key);
}
} //end elif L
else if (key > ptr->key)
{
if (ptr->right != nullptr)
{
AddLeafPrivate(key, ptr->right);
} // recursively checking right
else
{
ptr->right = CreateLeaf(key);
}
} //end elif R
else
{
cout << "The Key " << key << " Has already been added to the tree\n";
}
} //end AddLeafPrivate
void BST::PrintInOrder() { PrintInOrderPrivate(root); }
void BST::PrintInOrderPrivate(BST::node *ptr)
{
if (root != nullptr)
{
if (ptr->left != nullptr)
{
PrintInOrderPrivate(ptr->left);
} //step 1) of In-order
cout << ptr->key << " "; //step 2) of In-order
if (ptr->right != nullptr)
{
PrintInOrderPrivate(ptr->right);
} //step 3) of In-order
}
else
{
cout << "The tree is empty\n";
}
} //end PRintInOrderPrivate
BST::node *BST::ReturnNode(int key) { return ReturnNodePrivate(key, root); }
BST::node *BST::ReturnNodePrivate(int key, BST::node *ptr)
{
if (ptr != nullptr)
{
if (ptr->key == key)
{
return ptr;
}
else
{
if (key < ptr->key)
{
return ReturnNodePrivate(key, ptr->left);
}
else
{
return ReturnNodePrivate(key, ptr->right);
}
}
}
else
{
return nullptr;
}
} //end ReturnNodePrivate
int BST::ReturnRootKey()
{
if (root != nullptr)
{
return root->key;
}
else
{
return -1000;
}
} //end ReturnRootKey
void BST::PrintChildren(int key)
{
node *ptr = ReturnNode(key);
if (ptr != nullptr)
{
cout << "Parent Node = " << ptr->key << endl;
ptr->left == nullptr ? cout << "Left Child = Null\n" : cout << "Left Child = " << ptr->left->key << endl;
ptr->right == nullptr ? cout << "Right Child = Null\n" : cout << "Right Child = " << ptr->right->key << endl;
}
else
{
cout << "Key " << key << " is not in the tree\n";
}
}
int BST::FindSmallest() { return FindSmallestPrivate(root); }
int BST::FindSmallestPrivate(node *ptr)
{
if (root == nullptr)
{
cout << "The tree is empty\n";
return -1000;
}
else
{
if (ptr->left != nullptr)
{
return FindSmallestPrivate(ptr->left);
}
else
{
return ptr->key;
}
}
} //end of FindSmallestPrivate
void BST::RemoveNode(int key) { RemoveNodePrivate(key, root); } //end of RemoveNode
void BST::RemoveNodePrivate(int key, BST::node *parent)
{
if (root != nullptr)
{
if (root->key == key)
{
RemoveRootMatch();
}
else
{
if (key < parent->key && parent->left != nullptr)
{
parent->left->key == key ? RemoveMatch(parent, parent->left, true) : RemoveNodePrivate(key, parent->left);
}
else if (key > parent->key && parent->right != nullptr)
{
parent->right->key == key ? RemoveMatch(parent, parent->right, false) : RemoveNodePrivate(key, parent->right);
}
else
{
cout << "The key " << key << " was not in tree\n";
}
}
}
else
{
cout << "The tree is empty\n";
}
}
void BST::RemoveRootMatch()
{
if (root != nullptr)
{
node *delPtr = root;
int rootkey = root->key;
int smallestInRightSubTree;
//0 children
if (root->left == nullptr && root->right == nullptr)
{
root = nullptr;
delete delPtr;
}
// 1 child
else if (root->left == nullptr && root->right != nullptr)
{
root = root->right;
delPtr->right = nullptr;
delete delPtr;
cout << " the root node with key " << rootkey << " was deleted. "
<< "The new root contains key " << root->key << endl;
}
// 1 child, opposite side
else if (root->left != nullptr && root->right == nullptr)
{
root = root->right;
delPtr->left = nullptr;
delete delPtr;
cout << " the root node with key " << rootkey << " was deleted. "
<< "The new root contains key " << root->key << endl;
}
// 2 children
else
{
smallestInRightSubTree = FindSmallestPrivate(root->right);
RemoveNodePrivate(smallestInRightSubTree, root);
root->key = smallestInRightSubTree;
cout << " The root key containing " << rootkey << " was overwritten with key " << root->key << endl;
}
}
else
{
"Root node is not removable, the tree is empty";
}
} //end RemoveRootMatch
void BST::RemoveMatch(node *parent, node *match, bool left)
{
if (root != nullptr)
{
node *delPtr;
int matchKey = match->key;
int smallestInRightSubTree;
}
else
{
cout << "Cannot remove match, the tree is empty\n";
}
}
//13