/
BST.cpp
182 lines (149 loc) · 3.66 KB
/
BST.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
#include "BST.h"
//// public wethods
//! No-arg constructor. Initializes an empty BST
BST::BST(){
pRoot = NULL;
}
//! Copy constructor. Makes a complete copy of its argument
BST::BST(const BST & other){
this->pRoot = NULL;
if(other.pRoot){
this->pRoot = new BSTNode(*other.pRoot);
this->pRoot->left = NULL;
this->pRoot->right = NULL;
this->CopyChildren(this->pRoot, other.pRoot);
}
}
//! Destructor
BST::~BST(){
this->Clear();
}
//! Assignment operator. Makes a complete copy of its argument
//! @return Reference to oneself
BST& BST::operator =(const BST & other){
if(pRoot){
this->Clear();
}
if(other.pRoot){
this->pRoot = new BSTNode(*other.pRoot);
this->pRoot->left = NULL;
this->pRoot->right = NULL;
this->CopyChildren(this->pRoot, other.pRoot);
}
return *this;
}
//! @return a pointer to the root node of the tree, or NULL if the tree is empty.
//! @note This is useful for BST clients that need to traverse the tree.)
BSTNode* BST::GetRoot()const{
return pRoot;
}
//! @return true if the BST is empty, or false if the BST is not empty
bool BST::IsEmpty() const{
return (pRoot == NULL);
}
//! Removes all values from the BST
void BST::Clear(BSTNode* pStart){
BSTNode* pNode = (pStart != NULL) ? pStart : this->pRoot;
if(pNode){
if(pNode->left){
this->Clear(pNode->left);
}
if(pNode->right){
this->Clear(pNode->right);
}
if(pNode == pRoot){
this->pRoot = NULL;
}
delete pNode;
}
}
//! @return the number of values in the BST
int BST::GetSize(BSTNode* pStart, int size) const{
int iReturn = size;
BSTNode* pNode = (pStart != NULL) ? pStart : pRoot;
if(pNode){
iReturn++;
if(pNode->left){
iReturn += this->GetSize(pNode->left, 0);
}
if(pNode->right){
iReturn += this->GetSize(pNode->right, 0);
}
}
return iReturn;
}
//! Inserts value v into the BST
//!
//! @param v The new value being inserted
//!
//! @return a pointer to the newly inserted node, or NULL if v was already
//! in the tree (i.e., NULL is used to indicate a duplicate insertion)
BSTNode* BST::Insert(const std::string & v, BSTNode* pStart){
if(true){
if(!pRoot){
pRoot = new BSTNode(v);
return pRoot;
}else{
BSTNode* pNode = (pStart != NULL) ? pStart : pRoot;
int iCmp = v.compare(pNode->value);
if(iCmp < 0){
if(pNode->left){
return this->Insert(v, pNode->left);
}else{
pNode->left = new BSTNode(v);
return pNode->left;
}
}else if(iCmp > 0){
if(pNode->right){
return this->Insert(v, pNode->right);
}else{
pNode->right = new BSTNode(v);
return pNode->right;
}
}
}
}
return NULL;
}
//! Searches the tree for value v
//!
//! @param v The new value being searched for
//!
//! @return a pointer to the node containing v, or NULL if v is not in the tree
BSTNode* BST::Find(const std::string & v, BSTNode* pStart) const{
BSTNode* pNode = (pStart != NULL) ? pStart : pRoot;
if(pNode){
int iCmp = v.compare(pNode->value);
if(iCmp < 0){
if(pNode->left){
return this->Find(v, pNode->left);
}else{
return NULL;
}
}else if(iCmp > 0){
if(pNode->right){
return this->Find(v, pNode->right);
}else{
return NULL;
}
}else{
return pNode;
}
}
return NULL;
}
//// public methods
void BST::CopyChildren(BSTNode* pNode, BSTNode* pOrig){
if(pOrig->left){
pNode->left = new BSTNode(*pOrig->left);
pNode->left->left = NULL;
pNode->left->right = NULL;
this->CopyChildren(pNode->left, pOrig->left);
}
if(pOrig->right){
pNode->right = new BSTNode(*pOrig->right);
pNode->right->left = NULL;
pNode->right->right = NULL;
this->CopyChildren(pNode->right, pOrig->right);
}
}