-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryNodeTree.cpp
More file actions
374 lines (325 loc) · 11.6 KB
/
binaryNodeTree.cpp
File metadata and controls
374 lines (325 loc) · 11.6 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//Â Created by Frank M. Carrano and Timothy M. Henry.
//Â Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.
/** @file BinaryNodeTree.cpp */
#include "BinaryNodeTree.h"
#include "BinaryNode.h"
#include <iostream>
//////////////////////////////////////////////////////////////
// Protected Utility Methods Section
//////////////////////////////////////////////////////////////
template<class ItemType>
int BinaryNodeTree<ItemType>::getHeightHelper(std::shared_ptr<BinaryNode<ItemType>> subTreePtr) const
{
if (subTreePtr == nullptr)
return 0;
else
return 1 + max(getHeightHelper(subTreePtr->getLeftChildPtr()),
getHeightHelper(subTreePtr->getRightChildPtr()));
} // end getHeightHelper
template<class ItemType>
int BinaryNodeTree<ItemType>::getNumberOfNodesHelper(std::shared_ptr<BinaryNode<ItemType>> subTreePtr) const
{
if (subTreePtr == nullptr)
return 0;
else
return 1 + getNumberOfNodesHelper(subTreePtr->getLeftChildPtr())
+ getNumberOfNodesHelper(subTreePtr->getRightChildPtr());
} // end getNumberOfNodesHelper
template<class ItemType>
auto BinaryNodeTree<ItemType>::balancedAdd(std::shared_ptr<BinaryNode<ItemType>> subTreePtr,
std::shared_ptr<BinaryNode<ItemType>> newNodePtr)
{
if (subTreePtr == nullptr)
return newNodePtr;
else
{
auto leftPtr = subTreePtr->getLeftChildPtr();
auto rightPtr = subTreePtr->getRightChildPtr();
if (getHeightHelper(leftPtr) > getHeightHelper(rightPtr))
{
rightPtr = balancedAdd(rightPtr, newNodePtr);
subTreePtr->setRightChildPtr(rightPtr);
}
else
{
leftPtr = balancedAdd(leftPtr, newNodePtr);
subTreePtr->setLeftChildPtr(leftPtr);
} // end if
return subTreePtr;
} // end if
} // end balancedAdd
template<class ItemType>
std::shared_ptr<BinaryNode<ItemType>> BinaryNodeTree<ItemType>::moveValuesUpTree(std::shared_ptr<BinaryNode<ItemType>> subTreePtr)
{
auto leftPtr = subTreePtr->getLeftChildPtr();
auto rightPtr = subTreePtr->getRightChildPtr();
int leftHeight = getHeightHelper(leftPtr);
int rightHeight = getHeightHelper(rightPtr);
if (leftHeight > rightHeight)
{
subTreePtr->setItem(leftPtr->getItem());
leftPtr = moveValuesUpTree(leftPtr);
subTreePtr->setLeftChildPtr(leftPtr);
return subTreePtr;
}
else
{
if (rightPtr != nullptr)
{
subTreePtr->setItem(rightPtr->getItem());
rightPtr = moveValuesUpTree(rightPtr);
subTreePtr->setRightChildPtr(rightPtr);
return subTreePtr;
}
else
{
//this was a leaf!
// value not important
return nullptr;
} // end if
} // end if
} // end moveValuesUpTree
/** Depth-first search of tree for item.
@param subTreePtr tree to search.
@param target target item to find.
@param success communicate to client we found it.
@returns A pointer to node containing the item. */
template<class ItemType>
std::shared_ptr<BinaryNode<ItemType>> BinaryNodeTree<ItemType>::removeValue(std::shared_ptr<BinaryNode<ItemType>> subTreePtr,
const ItemType target,
bool& success)
{
if (subTreePtr == nullptr) // not found here
return subTreePtr;
if (subTreePtr->getItem() == target) // found it
{
subTreePtr = moveValuesUpTree(subTreePtr);
success = true;
return subTreePtr;
}
else
{
auto targetNodePtr = removeValue(subTreePtr->getLeftChildPtr(), target, success);
subTreePtr->setLeftChildPtr(targetNodePtr);
if (!success) // no need to search right subTree
{
targetNodePtr = removeValue(subTreePtr->getRightChildPtr(), target, success);
subTreePtr->setRightChildPtr(targetNodePtr);
} // end if
return subTreePtr;
} // end if
} // end removeValue
template<class ItemType>
auto BinaryNodeTree<ItemType>::findNode(std::shared_ptr<BinaryNode<ItemType>> treePtr,
const ItemType& target,
bool& success) const
{
if (treePtr == nullptr) // not found here
return treePtr;
if (treePtr->getItem() == target) // found it
{
success = true;
return treePtr;
}
else
{
std::shared_ptr<BinaryNode<ItemType>> targetNodePtr = findNode(treePtr->getLeftChildPtr(), target, success);
if (!success) // no need to search right subTree
{
targetNodePtr = findNode(treePtr->getRightChildPtr(), target, success);
} // end if
return targetNodePtr;
} // end if
} // end findNode
template<class ItemType>
std::shared_ptr<BinaryNode<ItemType>> BinaryNodeTree<ItemType>::copyTree(const std::shared_ptr<BinaryNode<ItemType>> oldTreeRootPtr) const
{
std::shared_ptr<BinaryNode<ItemType>> newTreePtr;
// Copy tree nodes during a preorder traversal
if (oldTreeRootPtr != nullptr)
{
// Copy node
newTreePtr = std::make_shared<BinaryNode<ItemType>>(oldTreeRootPtr->getItem(), nullptr, nullptr);
newTreePtr->setLeftChildPtr(copyTree(oldTreeRootPtr->getLeftChildPtr()));
newTreePtr->setRightChildPtr(copyTree(oldTreeRootPtr->getRightChildPtr()));
} // end if
return newTreePtr;
} // end copyTree
template<class ItemType>
void BinaryNodeTree<ItemType>::destroyTree(std::shared_ptr<BinaryNode<ItemType>> subTreePtr)
{
if (subTreePtr != nullptr)
{
destroyTree(subTreePtr->getLeftChildPtr());
destroyTree(subTreePtr->getRightChildPtr());
subTreePtr.reset(); // decrement reference count to node
} // end if
} // end destroyTree
//////////////////////////////////////////////////////////////
// Protected Tree Traversal Sub-Section
//////////////////////////////////////////////////////////////
template<class ItemType>
void BinaryNodeTree<ItemType>::preorder(void visit(ItemType&), std::shared_ptr<BinaryNode<ItemType>> treePtr) const
{
if (treePtr != nullptr)
{
ItemType theItem = treePtr->getItem();
visit(theItem);
// visit(treePtr->getItem());
preorder(visit, treePtr->getLeftChildPtr());
preorder(visit, treePtr->getRightChildPtr());
} // end if
} // end preorder
template<class ItemType>
void BinaryNodeTree<ItemType>::inorder(void visit(ItemType&), std::shared_ptr<BinaryNode<ItemType>> treePtr) const
{
if (treePtr != nullptr)
{
inorder(visit, treePtr->getLeftChildPtr());
ItemType theItem = treePtr->getItem();
visit(theItem);
inorder(visit, treePtr->getRightChildPtr());
} // end if
} // end inorder
template<class ItemType>
void BinaryNodeTree<ItemType>::postorder(void visit(ItemType&), std::shared_ptr<BinaryNode<ItemType>> treePtr) const
{
if (treePtr != nullptr)
{
postorder(visit, treePtr->getLeftChildPtr());
postorder(visit, treePtr->getRightChildPtr());
ItemType theItem = treePtr->getItem();
visit(theItem);
} // end if
} // end postorder
//////////////////////////////////////////////////////////////
// PUBLIC METHODS BEGIN HERE
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
// Constructor and Destructor Section
//////////////////////////////////////////////////////////////
template<class ItemType>
BinaryNodeTree<ItemType>::BinaryNodeTree()
{ } // end default constructor
template<class ItemType>
BinaryNodeTree<ItemType>::BinaryNodeTree(const ItemType& rootItem)
:rootPtr(std::make_shared<BinaryNode<ItemType>>(rootItem, nullptr, nullptr))
{ } // end constructor
template<class ItemType>
BinaryNodeTree<ItemType>::BinaryNodeTree(const ItemType& rootItem,
const std::shared_ptr<BinaryNodeTree<ItemType>> leftTreePtr,
const std::shared_ptr<BinaryNodeTree<ItemType>> rightTreePtr)
: rootPtr(std::make_shared<BinaryNode<ItemType>>(rootItem,
copyTree(leftTreePtr->rootPtr),
copyTree(rightTreePtr->rootPtr)))
{ } // end constructor
template<class ItemType>
BinaryNodeTree<ItemType>::BinaryNodeTree(const BinaryNodeTree<ItemType>& treePtr)
{
rootPtr = copyTree(treePtr.rootPtr);
} // end copy constructor
template<class ItemType>
BinaryNodeTree<ItemType>::~BinaryNodeTree()
{
destroyTree(rootPtr);
} // end destructor
//////////////////////////////////////////////////////////////
// Public BinaryTreeInterface Methods Section
//////////////////////////////////////////////////////////////
template<class ItemType>
bool BinaryNodeTree<ItemType>::isEmpty() const
{
return rootPtr == nullptr;
} // end isEmpty
template<class ItemType>
int BinaryNodeTree<ItemType>::getHeight() const
{
return getHeightHelper(rootPtr);
} // end getHeight
template<class ItemType>
int BinaryNodeTree<ItemType>::getNumberOfNodes() const
{
return getNumberOfNodesHelper(rootPtr);
} // end getNumberOfNodes
template<class ItemType>
void BinaryNodeTree<ItemType>::clear()
{
destroyTree(rootPtr);
rootPtr.reset();
} // end clear
template<class ItemType>
ItemType BinaryNodeTree<ItemType>::getRootData() const throw(PrecondViolatedExcep)
{
if (isEmpty())
throw PrecondViolatedExcep("getRootData() called with empty tree.");
return rootPtr->getItem();
} // end getRootData
template<class ItemType>
void BinaryNodeTree<ItemType>::setRootData(const ItemType& newItem)
{
if (isEmpty())
rootPtr = std::make_shared<BinaryNode<ItemType>>(newItem, nullptr, nullptr);
else
rootPtr->setItem(newItem);
} // end setRootData
template<class ItemType>
bool BinaryNodeTree<ItemType>::add(const ItemType& newData)
{
auto newNodePtr = std::make_shared<BinaryNode<ItemType>>(newData);
rootPtr = balancedAdd(rootPtr, newNodePtr);
return true;
} // end add
template<class ItemType>
bool BinaryNodeTree<ItemType>::remove(const ItemType& target)
{
bool isSuccessful = false;
rootPtr = removeValue(rootPtr, target, isSuccessful);
return isSuccessful;
} // end remove
template<class ItemType>
ItemType BinaryNodeTree<ItemType>::getEntry(const ItemType& anEntry) const throw(NotFoundException)
{
bool isSuccessful = false;
auto binaryNodePtr = findNode(rootPtr, anEntry, isSuccessful);
if (isSuccessful)
return binaryNodePtr->getItem();
else
throw NotFoundException("Entry not found in tree!");
} // end getEntry
template<class ItemType>
bool BinaryNodeTree<ItemType>::contains(const ItemType& anEntry) const
{
bool isSuccessful = false;
findNode(rootPtr, anEntry, isSuccessful);
return isSuccessful;
} // end contains
//////////////////////////////////////////////////////////////
// Public Traversals Section
//////////////////////////////////////////////////////////////
template<class ItemType>
void BinaryNodeTree<ItemType>::preorderTraverse(void visit(ItemType&)) const
{
preorder(visit, rootPtr);
} // end preorderTraverse
template<class ItemType>
void BinaryNodeTree<ItemType>::inorderTraverse(void visit(ItemType&)) const
{
inorder(visit, rootPtr);
} // end inorderTraverse
template<class ItemType>
void BinaryNodeTree<ItemType>::postorderTraverse(void visit(ItemType&)) const
{
postorder(visit, rootPtr);
} // end postorderTraverse
//////////////////////////////////////////////////////////////
// Overloaded Operator
//////////////////////////////////////////////////////////////
template<class ItemType>
BinaryNodeTree<ItemType>& BinaryNodeTree<ItemType>::operator=(
const BinaryNodeTree<ItemType>& rightHandSide)
{
if (!isEmpty())
clear();
this = copyTree(&rightHandSide);
return *this;
} // end operator=