-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeDB.cpp
75 lines (64 loc) · 1.81 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
#include "TreeDB.h"
TreeDB::TreeDB(){
root = NULL;
probesCount = 0;
}
TreeDB::~TreeDB(){
delete root;
root = NULL;
}
bool TreeDB::insert(DBentry* newEntry){
if(root==NULL){
root = new TreeNode(newEntry);
return true;
}
return root->insert(newEntry);
}
DBentry* TreeDB::find(string name){
probesCount = 0;
if(root==NULL) return NULL;
return root->find(name,probesCount);
}
bool TreeDB::remove(string name){
if(root==NULL) return false;
if(root->getEntry()->getName() == name){ //Removing the root
if(root->getLeft()==NULL){ //Right subtree is now the tree
TreeNode *temp = root->getRight();
root->setRight(NULL);
delete root;
root = temp;
}
else if(root->getRight()==NULL){ //Left subtree is now the tree
TreeNode *temp = root->getLeft();
root->setLeft(NULL);
delete root;
root = temp;
}
else //Has left and right subtrees, use the algorithm
(root->getLeft())->max(root);
return true;
}
//Will only get here if root is not the desired node
bool leftBool,rightBool;
if(root->getLeft()!=NULL) //Avoid dereferencing NULL pointer
leftBool = root->getLeft()->remove(name,root,0);
else leftBool = false;
if(root->getRight()!=NULL) ////Avoid dereferencing NULL pointer
rightBool = root->getRight()->remove(name,root,1);
else rightBool = false;
return (leftBool || rightBool);
}
void TreeDB::clear(){
delete root;
root = NULL; //Good practice
}
void TreeDB::printProbes() const{
cout << probesCount << endl;;
}
void TreeDB::countActive() const{
cout << root->countActive() << endl;
}
void TreeDB::print(){
if(root!=NULL)
root->print();
}