-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.cpp
More file actions
323 lines (280 loc) · 10.5 KB
/
BinarySearchTree.cpp
File metadata and controls
323 lines (280 loc) · 10.5 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
//Â Created by Frank M. Carrano and Timothy M. Henry.
//Â Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.
/** @file BinarySearchTree.cpp */
#include <iostream>
#include "BinarySearchTree.h"
#include "BinaryNode.h"
//////////////////////////////////////////////////////////////
//
// Protected Utility Methods Section
//
//////////////////////////////////////////////////////////////
template<class ItemType>
auto BinarySearchTree<ItemType>::placeNode(std::shared_ptr<BinaryNode<ItemType>> subTreePtr,
std::shared_ptr<BinaryNode<ItemType>> newNodePtr)
{
if (subTreePtr == nullptr)
return newNodePtr;
else
{
if (subTreePtr->getItem() > newNodePtr->getItem())
subTreePtr->setLeftChildPtr(placeNode(subTreePtr->getLeftChildPtr(), newNodePtr));
else
subTreePtr->setRightChildPtr(placeNode(subTreePtr->getRightChildPtr(), newNodePtr));
return subTreePtr;
} // end if
} // end placeNode
template<class ItemType>
std::shared_ptr<BinaryNode<ItemType>> BinarySearchTree<ItemType>::removeValue(std::shared_ptr<BinaryNode<ItemType>> subTreePtr,
const ItemType target,
bool& success)
{
if (subTreePtr == nullptr)
{
// Not found here
success = false;
return subTreePtr;
}
if (subTreePtr->getItem() == target)
{
// Item is in the root of some subtree
subTreePtr = removeNode(subTreePtr);
success = true;
return subTreePtr;
}
else
{
if (subTreePtr->getItem() > target)
{
// Search the left subtree
subTreePtr->setLeftChildPtr(removeValue(subTreePtr->getLeftChildPtr(), target, success));
}
else
{
// Search the right subtree
subTreePtr->setRightChildPtr(removeValue(subTreePtr->getRightChildPtr(), target, success));
}
return subTreePtr;
} // end if
} // end removeValue
template<class ItemType>
auto BinarySearchTree<ItemType>::removeNode(std::shared_ptr<BinaryNode<ItemType>> nodePtr)
{
// Case 1) Node is a leaf - it is deleted
// Case 2) Node has one child - parent adopts child
// Case 3) Node has two children:
// Traditional implementation: Find successor node.
// Alternate implementation: Find successor value and replace node's value;
// alternate does not need pass-by-reference
if (nodePtr->isLeaf())
{
nodePtr.reset();
return nodePtr; // delete and return nullptr
}
else if (nodePtr->getLeftChildPtr() == nullptr) // Has rightChild only
{
return nodePtr->getRightChildPtr();
}
else if (nodePtr->getRightChildPtr() == nullptr) // Has left child only
{
return nodePtr->getLeftChildPtr();
}
else // Has two children
{
// Traditional way to remove a value in a node with two children
ItemType newNodeValue;
nodePtr->setRightChildPtr(removeLeftmostNode(nodePtr->getRightChildPtr(), newNodeValue));
nodePtr->setItem(newNodeValue);
return nodePtr;
// Alernative way to remove a value in a node with two children; does not require pass-by-reference.
// We need to check whether this right child has a left child.
// This is similar to the base case in "findSuccessorValue" but we need to remove the
// special case where the right child *is* the inorder successor
/*
std::shared_ptr<BinaryNode<ItemType>> myRightChildPtr = nodePtr->getRightChildPtr();
std::shared_ptr<BinaryNode<ItemType>> myLeftGrandChildPtr = myRightChildPtr->getLeftChildPtr();
// Special case - right child is successor
if (myLeftGrandChildPtr == nullptr)
{
nodePtr->setItem(myRightChildPtr->getItem());
nodePtr->setRightChildPtr(removeNode(myRightChildPtr));
return nodePtr;
}
else
{
// Now we can recurse
nodePtr->setItem(findSuccessorValue(nodePtr->getRightChildPtr()));
return nodePtr;
} // end if
*/
} // end if
} // end removeNode
template<class ItemType>
auto BinarySearchTree<ItemType>::removeLeftmostNode(std::shared_ptr<BinaryNode<ItemType>> nodePtr,
ItemType& inorderSuccessor)
{
if (nodePtr->getLeftChildPtr() == nullptr)
{
inorderSuccessor = nodePtr->getItem();
return removeNode(nodePtr);
}
else
{
nodePtr->setLeftChildPtr(removeLeftmostNode(nodePtr->getLeftChildPtr(), inorderSuccessor));
return nodePtr;
} // end if
} // end removeLeftmostNode
/*
template<class ItemType>
ItemType BinarySearchTree<ItemType>::findSuccessorValue(std::shared_ptr<BinaryNode<ItemType>> subTreePtr)
{
std::shared_ptr<BinaryNode<ItemType>> myLeftChildPtr = subTreePtr->getLeftChildPtr();
if (myLeftChildPtr->getLeftChildPtr() == nullptr) {
ItemType nodeItemValue = myLeftChildPtr->getItem();
subTreePtr->setLeftChildPtr(removeNode(myLeftChildPtr));
return nodeItemValue;
}
else
{
return findSuccessorValue(subTreePtr->getLeftChildPtr());
} // end if
} // end findSuccessorValue
*/
// Override findNode because now we can use a binary search:
template<class ItemType>
auto BinarySearchTree<ItemType>::findNode(std::shared_ptr<BinaryNode<ItemType>> subTreePtr,
const ItemType& target) const
{
// Uses a binary search
if (subTreePtr == nullptr)
return subTreePtr; // Not found
else if (subTreePtr->getItem() == target)
return subTreePtr; // Found
else if (subTreePtr->getItem() > target)
// Search left subtree
return findNode(subTreePtr->getLeftChildPtr(), target);
else
// Search right subtree
return findNode(subTreePtr->getRightChildPtr(), target);
} // end findNode
//////////////////////////////////////////////////////////////
// PUBLIC METHODS BEGIN HERE
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
// Constructor and Destructor Section
//////////////////////////////////////////////////////////////
template<class ItemType>
BinarySearchTree<ItemType>::BinarySearchTree()
{ } // end default constructor
template<class ItemType>
BinarySearchTree<ItemType>::BinarySearchTree(const ItemType& rootItem)
: rootPtr(std::make_shared<BinaryNode<ItemType>>(rootItem, nullptr, nullptr))
{ } // end constructor
template<class ItemType>
BinarySearchTree<ItemType>::BinarySearchTree(const BinarySearchTree<ItemType>& treePtr)
{
rootPtr = this->copyTree(treePtr.rootPtr); // Call inherited method
} // end copy constructor
template<class ItemType>
BinarySearchTree<ItemType>::~BinarySearchTree()
{
this->destroyTree(rootPtr); // Call inherited method
} // end destructor
//////////////////////////////////////////////////////////////
// Public BinaryTreeInterface Methods Section
//////////////////////////////////////////////////////////////
template<class ItemType>
bool BinarySearchTree<ItemType>::isEmpty() const
{
return rootPtr == nullptr;
} // end isEmpty
template<class ItemType>
int BinarySearchTree<ItemType>::getHeight() const
{
return this->getHeightHelper(rootPtr); // Call inherited method
} // end getHeight
template<class ItemType>
int BinarySearchTree<ItemType>::getNumberOfNodes() const
{
return this->getNumberOfNodesHelper(rootPtr); // Call inherited method
} // end getNumberOfNodes
template<class ItemType>
void BinarySearchTree<ItemType>::clear()
{
this->destroyTree(rootPtr); // Call inherited method
rootPtr.reset();
} // end clear
template<class ItemType>
ItemType BinarySearchTree<ItemType>::getRootData() const throw(PrecondViolatedExcep)
{
if (isEmpty())
throw PrecondViolatedExcep("getRootData() called with empty tree.");
return rootPtr->getItem();
} // end getRootData
// Must override setRootData to disable its affect:
template<class ItemType>
void BinarySearchTree<ItemType>::setRootData(const ItemType& newItem) const throw(PrecondViolatedExcep)
{
throw PrecondViolatedExcep("Cannot change root value in a BST!");
} // end setRootData
template<class ItemType>
bool BinarySearchTree<ItemType>::add(const ItemType& newData)
{
auto newNodePtr = std::make_shared<BinaryNode<ItemType>>(newData);
rootPtr = placeNode(rootPtr, newNodePtr);
return true;
} // end add
template<class ItemType>
bool BinarySearchTree<ItemType>::remove(const ItemType& target)
{
bool isSuccessful = false;
// call may change isSuccessful
rootPtr = removeValue(rootPtr, target, isSuccessful);
return isSuccessful;
} // end remove
// Override getEntry to use our improved findNode:
template<class ItemType>
ItemType BinarySearchTree<ItemType>::getEntry(const ItemType& anEntry) const throw(NotFoundException)
{
std::shared_ptr<BinaryNode<ItemType>> nodeWithEntry = findNode(rootPtr, anEntry);
if (nodeWithEntry == nullptr)
throw NotFoundException("Entry not found in tree.");
else
return nodeWithEntry->getItem();
} // end getEntry
// Override contains to use our improved findNode:
template<class ItemType>
bool BinarySearchTree<ItemType>::contains(const ItemType& anEntry) const
{
return (findNode(rootPtr, anEntry) != nullptr); // nullptr is same as false
} // end contains
//////////////////////////////////////////////////////////////
// Public Traversals Section
//////////////////////////////////////////////////////////////
template<class ItemType>
void BinarySearchTree<ItemType>::preorderTraverse(void visit(ItemType&)) const
{
this->preorder(visit, rootPtr); // Call inherited method
} // end preorderTraverse
template<class ItemType>
void BinarySearchTree<ItemType>::inorderTraverse(void visit(ItemType&)) const
{
this->inorder(visit, rootPtr); // Call inherited method
} // end inorderTraverse
template<class ItemType>
void BinarySearchTree<ItemType>::postorderTraverse(void visit(ItemType&)) const
{
this->postorder(visit, rootPtr); // Call inherited method
} // end postorderTraverse
//////////////////////////////////////////////////////////////
// Overloaded Operator
//////////////////////////////////////////////////////////////
template<class ItemType>
BinarySearchTree<ItemType>& BinarySearchTree<ItemType>::
operator=(const BinarySearchTree<ItemType>& rightHandSide)
{
if (!isEmpty())
clear();
this = copyTree(&rightHandSide); // Call inherited method
return *this;
} // end operator=