-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.h
More file actions
183 lines (160 loc) · 5.29 KB
/
Copy pathBinarySearchTree.h
File metadata and controls
183 lines (160 loc) · 5.29 KB
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
#ifndef PROJECT3STARTER_BINARYSEARCHTREE_H
#define PROJECT3STARTER_BINARYSEARCHTREE_H
#include <iostream>
#include <memory>
#include <ostream>
template <typename Comparable>
class BinarySearchTree {
private:
struct BinaryNode {
Comparable value;
BinaryNode* leftChild;
BinaryNode* rightChild;
// Constructors
BinaryNode() : value(Comparable()), leftChild(nullptr), rightChild(nullptr) {}
explicit BinaryNode(Comparable c) : value(c), leftChild(nullptr), rightChild(nullptr) {}
BinaryNode(Comparable c, BinaryNode* l, BinaryNode* r) : value(c), leftChild(l), rightChild(r) {}
};
BinaryNode* root;
unsigned long size;
// Helper recursive function to destroy the tree.
void destroy(BinaryNode* &n) {
if (n != nullptr) {
destroy(n->leftChild);
destroy(n->rightChild);
delete n;
n = nullptr;
}
}
// Helper recursive function to copy the tree.
BinaryNode* copyNode(BinaryNode* n) {
if (n == nullptr) {
return nullptr;
}
++size;
return new BinaryNode(n->value, copyNode(n->leftChild), copyNode(n->rightChild));
}
// Helper recursive function to find a value in the tree.
int find(const Comparable &c, BinaryNode* n, int & count) const {
if (n == nullptr) {
// Reached a dead end. Value not in tree.
return count;
}
if (c < n->value) {
// Value is less than current node. Go to node's left child.
return find(c, n->leftChild, ++count);
}
if (n->value < c) {
// Value is greater than current node. Go to node's right child.
return find(c, n->rightChild, ++count);
}
// If code reaches here, c == n->value. Node found!
//std::cout << count << std::endl;
return count;
}
// Helper recursive function to add a value to the tree.
bool add(const Comparable &c, BinaryNode* &n) {
if (n == nullptr) {
// We found the place where we can add the node.
n = new BinaryNode(c, nullptr, nullptr);
++size;
return true;
}
else if (c < n->value) {
// Value is less than current node. Go to left child.
return add(c, n->leftChild);
}
else if (n->value < c) {
// Value is greater than current node. Go to right child.
return add(c, n->rightChild);
}
// If code reaches here, value is a duplicate. Cannot add it to the tree.
return false;
}
// Helper recursive method to find the maximum value from a given node.
Comparable& findMax(BinaryNode* n) {
if (n->rightChild == nullptr) {
return n->value;
}
return findMax(n->rightChild);
}
// Helper recursive function to delete a value from the tree.
void remove(const Comparable &c, BinaryNode* &n) {
if (n == nullptr) {
// We did not find the value. Cannot remove it. Nothing to do.
return;
}
else if (c < n->value) {
// Value is less than current node. Go to left child.
remove(c, n->leftChild);
}
else if (n->value < c) {
// Value is greater than current node. Go to right child.
remove(c, n->rightChild);
}
// If code reaches here, we found the node. Now to remove it.
else if (n->leftChild != nullptr && n->rightChild != nullptr) {
// The node we want to remove has two children
// Find the largest value from the left subtree
n->value = findMax(n->leftChild);
remove(n->value, n->leftChild);
}
else {
// The node we want to remove has 0 or 1 child.
// If it has a child, move it up. If not, set to nullptr.
BinaryNode *oldNode = n;
n = (n->leftChild != nullptr) ? n->leftChild : n->rightChild;
delete oldNode;
oldNode = nullptr;
--size;
}
}
public:
// Default Constructor
BinarySearchTree() {
root = nullptr;
size = 0;
}
// Copy Constructor
BinarySearchTree(const BinarySearchTree &b) {
size = 0;
// calls private helper function
root = copyNode(b.root);
}
// Destructor
~BinarySearchTree() {
// calls private helper function
destroy(root);
}
// Method to destroy tree
void timber() {
// calls private helper function
destroy(root);
}
bool isEmpty() const {
return (root == nullptr);
}
int find(const Comparable &c) const {
int rootD = 0;
// calls private helper function
return this->find(c, root, rootD);
}
bool add(const Comparable &c) {
// calls private helper function
return add(c, root);
}
void remove(const Comparable &c) {
// calls private helper function
remove(c, root);
}
unsigned long getSize() const {
return size;
}
// Overloaded = operator
BinarySearchTree& operator = (const BinarySearchTree &rhs) {
destroy(root);
size = 0;
root = copyNode(rhs.root);
}
};
#endif //PROJECT3STARTER_BINARYSEARCHTREE_H