-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeDB.cpp
149 lines (132 loc) · 3.78 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
#include "TreeDB.h"
//constructor
TreeDB::TreeDB() {
root = NULL;
}
//destructor
TreeDB::~TreeDB() {
if (root = NULL)
return;
clearHelp(root);
}
//inserts the entry pointed to by newEntry into the database.
bool TreeDB::insert(DBentry* newEntry) {
TreeNode* temp = new TreeNode(newEntry);
if (root == NULL) {
root = temp;
return true;
}
return (insertHelp(root, temp));
}
//function to decide which subtree to go down and insert the newEntry into.
bool TreeDB:: insertHelp(TreeNode* node, TreeNode* temp) {
string newName = temp -> getEntry() -> getName();
string currentNodeName = node -> getEntry() -> getName();
if (newName.compare(currentNodeName) == 0) {
delete temp;
return false;
}
else if (newName.compare(currentNodeName) > 0) {
if (node -> getRight() == NULL) {
node -> setRight(temp);
return true;
}
else
return insertHelp(node -> getRight(), temp);
}
else {
if (node -> getLeft() == NULL) {
node -> setLeft(temp);
return true;
}
else
return insertHelp(node -> getLeft(), temp);
}
}
//searches the database for an entry with a key equal to name.
DBentry* TreeDB::find(string name) {
if (root == NULL)
return NULL;
probesCount = 0;
return (findHelp(root, name));
}
//function to find where in TreeDB the entry with name is, or if it exists.
DBentry* TreeDB::findHelp(TreeNode* node, string name) {
string currentNodeName = node -> getEntry() -> getName();
if (name.compare(currentNodeName) == 0) {
probesCount++;
return node -> getEntry();
}
else if (name.compare(currentNodeName) > 0) {
if (node -> getRight() == NULL)
return NULL;
else {
probesCount++;
return findHelp(node -> getRight(), name);
}
}
else {
if (node -> getLeft() == NULL)
return NULL;
else {
probesCount++;
return findHelp(node -> getLeft(), name);
}
}
}
//deletes the entry with the specified name (key) from the database.
bool TreeDB::remove(string name) {
if (root == NULL)
return false;
return root -> remove(name, root);
}
//deletes all the entries in the database.
void TreeDB::clear() {
if (root == NULL)
return;
clearHelp (root);
root = NULL;
}
//function deletes all the entries from the bottom subtrees up
void TreeDB::clearHelp(TreeNode* node) {
if (node == NULL)
return;
clearHelp(node -> getLeft());
clearHelp(node -> getRight());
delete node;
node = NULL;
}
//prints the number of probes stored in probesCount
void TreeDB::printProbes() const {
cout << probesCount << endl;
}
//computes and prints out the total number of active entries in the database
void TreeDB::countActive() const {
int count = 0;
countActiveHelp(root, count);
cout << count << endl;
}
//function traverses through the TreeDB to find which nodes are active
void TreeDB::countActiveHelp(TreeNode* node, int& ACount) const {
if (node == NULL)
return;
countActiveHelp (node -> getLeft(), ACount);
countActiveHelp (node -> getRight(), ACount);
if (node -> getEntry() -> getActive())
ACount++;
}
// Prints the entire tree, in ascending order of key/name
ostream& operator << (ostream& out, const TreeDB& rhs) {
if (rhs.root == NULL)
return out;
rhs.printAllHelp(rhs.root);
return out;
}
//function traverses the entire tree and prints all the nodes
void TreeDB::printAllHelp(TreeNode* node) const {
if (node == NULL)
return;
printAllHelp(node -> getLeft());
cout << *(node -> getEntry());
printAllHelp(node -> getRight());
}