-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSTree.cpp
231 lines (217 loc) · 5.62 KB
/
BSTree.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "BSTree.h"
BSTree::BSTree( ) {
root = nullptr;
}
BSTree::~BSTree() {
del(root);
}
void BSTree::del(Node* ptr) { // destructor helper function
if(!ptr)
return;
del(ptr->left);
del(ptr->right);
delete ptr;
}
void BSTree::insert(const string& key) {
if(!root){ // base case: empty tree
Node* newNode = new Node(key);
root = newNode;
return;
}
Node* ptr = root; // previous node
Node* prevPtr = nullptr;
while(ptr) {
prevPtr = ptr; //saves previous node
if(ptr->getValue() > key)
ptr = ptr->left;
else if(ptr->getValue() < key)
ptr = ptr->right;
else if(ptr->getValue() == key){ // duplicate key
ptr->increment();
return;
}
}
//if tree is traversed and key is in the leaf nodes
Node* newNode = new Node(key);
if(prevPtr->getValue() > key)
prevPtr->left = newNode;
else
prevPtr->right = newNode;
}
void BSTree::remove(const string& key) {
remove(nullptr, root, key);
}
void BSTree::remove(Node* parent, Node* ptr, const string& key) {
if(!ptr)
return;
if(ptr->getValue() > key) // target node is on the left
return remove(ptr, ptr->left, key);
if(ptr->getValue() < key) // target node is on the right
return remove(ptr, ptr->right, key);
// we have arrived at target node
// case: count > 1
if(ptr->getCount() > 1) {
ptr->decrement();
return;
}
// case: no children
if(!ptr->left && !ptr->right) {
if(parent) {
if(parent->left == ptr) parent->left = nullptr;
else parent->right = nullptr;
}
else root = nullptr;
delete ptr;
return;
}
// case: one child
if (!ptr->left || !ptr->right) {
if(ptr->right) {
if(ptr->right->left) {
Node* rc = ptr->right;
Node* lc = rc->left;
rc->left = lc->right;
lc->right = rc;
parent->left = lc;
}
else {
if(parent) {
if(parent->left == ptr)
parent->left = ptr->right;
else
parent->right = ptr->right;
}
else {
root = ptr->right;
}
}
}
else {
if(parent) {
if(parent->left == ptr) parent->left = ptr->left;
else parent->right = ptr->left;
}
else {
root = ptr->left;
}
}
delete ptr;
return;
}
// case: two children
// find the node to replace ptr with: rightmost child of left subtree
Node* n = ptr->left;
Node* prevN = ptr;
while(n->right) {
prevN = n;
n = n->right;
}
if(prevN->right == n) { // if we actually went all the way to the right
prevN->right = n->left;
n->left = ptr->left;
}
else{ // account for zybooks
Node* rightMostN = n->left;
Node* prevNn = n;
if(rightMostN){
while(rightMostN->right){
prevNn = rightMostN;
rightMostN = rightMostN->right;
}
if(rightMostN != n->left){
prevNn->right = rightMostN->left;
rightMostN->left = n->left;
n->left = rightMostN;
}
}
}
n->right = ptr->right;
// replace ptr with n
if(parent) {
if(parent->left == ptr) parent->left = n;
else parent->right = n;
}
else root = n;
// delete ptr
delete ptr;
}
bool BSTree::search(const string& key) const { //needs work
return search(root, key);
}
bool BSTree::search(Node* ptr, const string& key) const {//needs work
if(!ptr)
return false;
if(ptr->getValue() == key)
return true;
if(ptr->getValue() > key)
return search(ptr->left, key);
return search(ptr->right, key);
}
string BSTree::largest() const {
//find rightmost node
Node* ptr = root;
if(!ptr)
return "";
while(ptr->right) {
ptr = ptr->right;
}
return ptr->getValue();
}
string BSTree::smallest() const {
//find leftmost node
Node* ptr = root;
if(!ptr)
return "";
while(ptr->left) {
ptr = ptr->left;
}
return ptr->getValue();
}
int BSTree::height(const string& key) const {
if (search(key)){ // if this key exists, find its height
Node* n = root;
while(n->getValue() != key){
if(n->getValue() < key)
n = n->right;
else
n = n->left;
}
return height(n);
}
return -1;
}
int BSTree::height(Node* ptr) const {
if(!ptr)
return -1;
return 1 + max(height(ptr->left), height(ptr->right));
}
void BSTree::inOrder() const {
inOrder(root);
}
void BSTree::inOrder(Node* ptr) const {
if(!ptr)
return;
inOrder(ptr->left);
cout << ptr->getValue() << "(" << ptr->getCount() << ")" << ", ";
inOrder(ptr->right);
}
void BSTree::preOrder() const {
preOrder(root);
}
void BSTree::preOrder(Node *ptr) const {
if(ptr) {
cout << ptr->getValue() << "(" << ptr->getCount() << ")" << ", ";
preOrder(ptr->left);
preOrder(ptr->right);
}
}
void BSTree::postOrder() const {
postOrder(root);
}
void BSTree::postOrder(Node *ptr) const {
if(ptr) {
postOrder(ptr->left);
postOrder(ptr->right);
cout << ptr->getValue() << "(" << ptr->getCount() << ")" << ", ";
}
}