-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeDB.cpp
executable file
·207 lines (181 loc) · 5.38 KB
/
TreeDB.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
/*
* File: TreeDB.cpp
* Author: catherinehuang
*
* Created on November 15, 2015, 7:44 PM
*/
#include "TreeDB.h"
#include <cstdlib>
using namespace std;
TreeDB::TreeDB() {
root = NULL;
probesCount = 0;
}
TreeDB::TreeDB(const TreeDB& orig) {
}
TreeDB::~TreeDB() {
}
DBentry* TreeDB::find(string name){
return findHelper(name, root);
}
DBentry* TreeDB::findHelper(string name, TreeNode* node){
probesCount++;
if (node == NULL){
probesCount = 0; // cannot find the node; set counter back to zero
return NULL;
}
// cout << "find: addr of current node = " << node << endl;
// cout << "find: inside node = " << *(node->getEntry()) ;
if (node->getEntry()->getName() == name)
return node->getEntry();
if (node->getEntry()->getName() > name){
//recursion through left tree
return findHelper (name, node->getLeft());
}
if (node->getEntry()->getName() < name){
//recursion through right tree
return findHelper (name, node->getRight());
}
return NULL;
}
bool TreeDB::insert(DBentry* newEntry){
if (root == NULL){
root = new TreeNode (newEntry);
return true;
}
return insertHelper(newEntry, root);
}
bool TreeDB::insertHelper(DBentry* entry, TreeNode *node){
// cout << "insert: goint down" << endl;
// cout << *entry ;
if (entry->getName() == node->getEntry()->getName()){
delete entry;
return false;
}
if (entry->getName() < node->getEntry()->getName()){
//go to left subtree
if (node->getLeft() == NULL){
node->setLeft(new TreeNode(entry));
return true;
}
else{
return insertHelper(entry, node->getLeft());
}
}
else{
//go to right subtree
if (node->getRight() == NULL){
node->setRight(new TreeNode(entry));
return true;
}
else{
return insertHelper(entry, node->getRight());
}
}
}
void TreeDB::remove(string name){
TreeNode *node = removeHelper(name, root);
}
TreeNode* TreeDB::removeHelper(string name, TreeNode* node){
//if node is null - end of the tree
if (node == NULL){
cout << "Error: entry does not exist" << endl;
return NULL;
}
if (name < node->getEntry()->getName()){
//if you are less than me, go to my left
node->setLeft(removeHelper(name, node->getLeft()));
return node;
}
if (name > node->getEntry()->getName()){
//if you are more than me, go to my right
node->setRight(removeHelper(name, node->getRight()));
return node;
}
//when it gets to this point, name is matched with an existing entry
//there's no right subtree
//go check my left subtree and find the biggest in there
if (node->getLeft() != NULL){
node->setEntry(findMax(node->getLeft()));
node->setLeft(removeHelper(node->getEntry()->getName(), node->getLeft()));
return node;
}
//now find my replacement from right subtree
if (node->getRight() != NULL){
node->setEntry(findMin(node->getRight()));
//steal the identity of that poor leaf node who will be mercilessly killed
//after it's exploited
node->setRight(removeHelper(node->getEntry()->getName(), node->getRight()));
//now find the residence of that node, and stand at its door
return node;
}
delete node;
//assassinate the leaf node
cout << "Success" << endl;
return NULL;
//bye
}
DBentry* TreeDB::findMax(TreeNode* node) const{
if (node->getRight() == NULL)
return node->getEntry();
return findMax(node->getRight());
}
DBentry* TreeDB::findMin(TreeNode* node) const{
if (node->getLeft() == NULL)
return node->getEntry();
return findMin(node->getLeft());
}
void TreeDB::print() const{
// cout << "going to print" << endl;
printHelper (root);
}
void TreeDB::printHelper(TreeNode* node) const{
if (node == NULL) // stops when you reach the end from a branch
return;
printHelper(node->getLeft());
cout << *(node->getEntry());
printHelper(node->getRight());
}
void TreeDB::countActive() const{
int num;
num = countActiveHelper(root);
cout << num << endl;
}
int TreeDB::countActiveHelper(TreeNode* node) const{
if(node == NULL) //tree is empty
return 0;
else if (node->getLeft() == NULL && node->getRight() == NULL)
//I'm a leaf
if(node->getEntry()->getActive())
return 1;
else
return 0;
else{
if (node->getEntry()->getActive())
return countActiveHelper(node->getLeft()) + countActiveHelper(node->getRight()) + 1;
return countActiveHelper(node->getLeft()) + countActiveHelper(node->getRight());
}
}
void TreeDB::printProbes(){
if (probesCount == 0)
return;
cout << probesCount << endl;
probesCount = 0;
}
void TreeDB::removeAll(){
if (root != NULL)
removeAllHelper(root);
cout << "Success" << endl;
root = NULL;
}
void TreeDB::removeAllHelper(TreeNode* node){
// //stopping condition
// cout << "travsering tree Node at: ";
// cout << *(node->getEntry());
// cout << "current addr of node = " << node << endl;
if (node == NULL)
return;
removeAllHelper(node->getLeft());
removeAllHelper(node->getRight());
delete node;
}