-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.cpp
More file actions
236 lines (209 loc) · 5.1 KB
/
Copy pathBinarySearchTree.cpp
File metadata and controls
236 lines (209 loc) · 5.1 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "BinarySearchTree.h"
#include <stdio.h>
#include <assert.h> /* assert */
template<class T>
BinarySearchTree<T>::BinarySearchTree()
{
m_root = new BinaryNode<T>(-1);
m_root->setToRoot();
}
template<class T>
void BinarySearchTree<T>::insert( T value )
{
//Find where to add it
if( m_root->getLeftChild() == nullptr ) //Root only uses left ptr
{
BinaryNode<T>* newNode = new BinaryNode<T>( value );
m_root->setLeftChild( newNode );
}
else
{
insert( m_root->getLeftChild(), value );
}
}
template<class T>
void BinarySearchTree<T>::insert( BinaryNode<T>* root, T value )
{
if( value < root->getValue() )
{
BinaryNode<T>* left = root->getLeftChild();
if( left == nullptr )
{
BinaryNode<T>* newNode = new BinaryNode<T>( value );
root->setLeftChild( newNode );
}
else
{
assert( left->getValue() <= root->getValue() );
insert( left, value );
}
}
else if( value > root->getValue() ) //root->getValue() > value
{
BinaryNode<T>* right = root->getRightChild();
if( right == nullptr )
{
BinaryNode<T>* newNode = new BinaryNode<T>( value );
root->setRightChild( newNode );
}
else
{
assert( right->getValue() > root->getValue() );
insert( right, value );
}
}
else
{
//Value to be inserted already exists => do nothing because only unique values are allowed
}
}
//The number of nodes that have been steped over before finding a node with the given value as value
template<class T>
int BinarySearchTree<T>::stepsTo( T value ) const
{
struct distanceMeasurer
{
distanceMeasurer(T v): isFound(false), steps(0), value(v) { }
void operator()( BinaryNode<T>* n )
{
if( n->getValue() == value )
{
isFound = true;
}
if( !isFound )
{
++steps;
}
}
bool isFound;
int steps;
T value;
};
distanceMeasurer result = forEach( distanceMeasurer( value ) );
if( result.isFound )
{
return result.steps;
}
else
{
return -1;
}
}
/*
template<class T>
void BinarySearchTree<T>::remove( T value )
{
BinaryNode<T>* ptr = m_root;
if( ptr->getLeftChild() != nullptr )
{
remove( ptr, value );
}
}
*/
/*
template<class T>
void BinarySearchTree<T>::remove( BinaryNode<T>* root, T value )
{
if( root == nullptr ) return;
//Find node to remove, n1 and its parent p1
//Find node to replace with, n2 and its parent p2
//Change p2 to point to null instead of n2
//Change p1 to point to n2 instead of n1
//Change n2 to point to same children as n1
BinaryNode<T>* n1,p1,n2,p2;
}
*/
template<class T>
void BinarySearchTree<T>::print()
{
struct Printer
{
Printer(){ }
void operator()( BinaryNode<T>* n ) { std::cout << " " << n->getValue() << ", "; }
};
forEach( Printer() );
}
template<class T>
int BinarySearchTree<T>::getNumElements() const
{
struct ElementCounter
{
ElementCounter(): num(0) { }
void operator()( BinaryNode<T>* ) { ++num; }
int num;
};
ElementCounter result = forEach( ElementCounter() );
return result.num;
}
template<class T>
T BinarySearchTree<T>::getSum() const
{
struct Summer
{
Summer(): sum(0) { }
void operator()( BinaryNode<T>* n ) { sum += n->getValue(); }
T sum;
};
Summer sum = forEach( Summer() );
return sum.sum;
}
template<class T>
T BinarySearchTree<T>::getMax() const
{
BinaryNode<T>* node = m_root->getLeftChild();
if( node == nullptr ) return -1;
while( node->getRightChild() != nullptr )
{
node = node->getRightChild();
}
return node->getValue();
}
template<class T>
T BinarySearchTree<T>::getMin() const
{
BinaryNode<T>* node = m_root->getLeftChild();
if( node == nullptr ) return -1;
while( node->getLeftChild() != nullptr )
{
node = node->getLeftChild();
}
return node->getValue();
}
/*
template<class T>
template<class UnaryFunction>
UnaryFunction BinarySearchTree<T>::forEach( UnaryFunction f ) const
{
forEachRecursive( m_root, f );
return f;
}
template<class T>
template<class UnaryFunction>
void BinarySearchTree<T>::forEachRecursive( BinaryNode<T>* node, UnaryFunction& f ) const
{
if( node == nullptr ) return;
forEachRecursive( node->getLeftChild(), f );
f( node->getValue() );
forEachRecursive( node->getRightChild(), f );
}
*/
/*
template<class T>
template<class T2>
void BinarySearchTree<T>::forEach( T2& sum ) const
{
forEachRecursive( m_root->getLeftChild(), sum );
}
*/
/*
template<class T>
template<class T2>
void BinarySearchTree<T>::forEachRecursive( BinaryNode<T>* node, T2& sum ) const
{
if( node == nullptr ) return;
forEachRecursive( node->getLeftChild(), sum );
sum += node->getValue();
forEachRecursive( node->getRightChild(), sum );
}
*/
template class BinarySearchTree<int>;