-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST312.h
434 lines (356 loc) · 10.2 KB
/
BST312.h
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* BST312.h
CS 312 Fall 2018
a simple implementation of a binary search tree
Ana Ashrafi
Section 16225
EID aa76288
*/
#ifndef BST_312_H
#define BST_312_H
#include <cstdlib> // Provides size_t
#include <iostream>
#include <vector>
using namespace std;
template<class ItemType>
class BST_312 {
public:
BST_312();
//Copy constructor
BST_312(const BST_312 & src);
~BST_312();
/****************************
makeEmpty
Function: Removes all the items from the BST.
Preconditions: BST has been initialized
Postconditions: All the items have been removed
*****************************/
void makeEmpty();
/****************************
isEmpty
Function: Checks to see if there are any items in the BST.
Preconditions: BST has been initialized
Postconditions: Returns true if there are no items in the BST, else false.
*****************************/
bool isEmpty() const;
/****************************
isFull
Function: Determines if you have any more room to add items to the BST
Preconditions: BST has been initialized
Postconditions: Returns true if there is no more room to add items, else false
*****************************/
bool isFull() const;
/****************************
insertItem
Function: Adds newItem to the BST.
Preconditions: BST has been initialized and is not full. Only unique values may be inserted (no duplicates)
Postconditions: newItem is in the proper position for a BST.
*****************************/
void insertItem(const ItemType &);
/****************************
deleteItem
Function: Removes target from BST.
Preconditions: BST has been initialized.
Postconditions: If found, element has been removed from BST.
*****************************/
void deleteItem(const ItemType& newItem);
/****************************
countNodes
Function: Counts the number of nodes in the BST
Preconditions: BST has been initialized.
Postconditions: returns the number of nodes in the BST
*****************************/
int countNodes();
/****************************
preOrderTraversal
Function: Returns the preOder (Node, Left, Right) as a vector of ItemTypes
Preconditions: BST has been initialized.
Postconditions: none
*****************************/
vector<ItemType> preOrderTraversal();
/****************************
inOrderTraversal
Function: Returns the inOder (Left, Node, Right) as a vector of ItemTypes
Preconditions: BST has been initialized.
Postconditions: none
*****************************/
vector<ItemType> inOrderTraversal();
/****************************
postOrderTraversal
Function: returns the postOder (Left, Right, Node) as a vector of ItemTypes
Preconditions: BST has been initialized.
Postconditions: none
*****************************/
vector<ItemType> postOrderTraversal();
/********************
isItemInTree
Function: Determines if a given Item is in tree.
Preconditions: BST has been initialized.
Postconditions: none
*****************************/
bool isItemInTree(const ItemType& item);
private:
struct TreeNode {
ItemType data;
TreeNode *left;
TreeNode *right;
};
TreeNode * root;
void insertItem(TreeNode*& t, const ItemType& newItem);
void inOrderTraversal(TreeNode* t,vector<ItemType>& result) const;
int countNodes(TreeNode* t) const;
void deleteNode(TreeNode*& node);
void makeEmpty(TreeNode*& t);
void deleteItem(TreeNode*& t, const ItemType& newItem);
void getPredecessor(TreeNode* t, ItemType& data);
void preOrderTraversal(TreeNode* t,vector<ItemType>& result) const;
void postOrderTraversal(TreeNode* t,vector<ItemType>& result) const;
void copyTree(TreeNode*& copy, const TreeNode *originalTree);
};
/*******************************
//Function implementations
********************************/
//Constructor
template<class ItemType>
BST_312<ItemType>::BST_312 ()
{
root = NULL;
}
//Copy Constructor
template<class ItemType>
BST_312<ItemType>::BST_312(const BST_312 & src)
{
copyTree(root, src.root);
}
//Destructor
template<class ItemType>
BST_312<ItemType>::~BST_312()
{
makeEmpty();
}
//Function to copy tree for copy constructor
template<class ItemType>
void BST_312<ItemType>::copyTree(TreeNode*& copy, const TreeNode* originalTree)
{
if (originalTree == NULL)
copy = NULL;
else
{
copy = new TreeNode;
copy->data = originalTree->data;
copyTree(copy->left, originalTree->left);
copyTree(copy->right, originalTree->right);
}
}
//Delete node from the BST
template<class ItemType>
void BST_312 <ItemType>::deleteNode(TreeNode*& t)
{
ItemType info;
TreeNode *tempPtr;
tempPtr = t;
if (t->left == NULL)
{
t = t->right;
delete tempPtr;
}
else if (t->right == NULL)
{
t = t->left;
delete tempPtr;
}
else
{
getPredecessor(t->left, info);
t->data = info;
deleteItem(t->left, info);
}
}
//Find largest in left subtree
template<class ItemType>
void BST_312 <ItemType>::getPredecessor(TreeNode* t, ItemType& data)
{
while (t->right != NULL)
t = t->right;
data = t->data;
}
//Delete given node from the BST
template<class ItemType>
void BST_312 <ItemType>::deleteItem(TreeNode*& t, const ItemType& newItem)
{
if (t == NULL)
return;
else if (newItem < t->data)
deleteItem(t->left, newItem);
else if (newItem > t->data)
deleteItem(t->right, newItem);
else
deleteNode(t);
}
template<class ItemType>
void BST_312 <ItemType>::deleteItem(const ItemType& newItem)
{
deleteItem(root, newItem);
}
//Delete the whole tree (free all nodes)
template<class ItemType>
void BST_312 <ItemType>::makeEmpty(TreeNode*& t)
{
if (t != NULL)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
}
template<class ItemType>
void BST_312 <ItemType>::makeEmpty()
{
makeEmpty(root);
root = NULL;
}
//Check if tree is empty
template<class ItemType>
bool BST_312 <ItemType>::isEmpty() const
{
return root == NULL;
}
//Check if tree is full
template<class ItemType>
bool BST_312 <ItemType>::isFull() const
{
try
{
TreeNode *temp = new TreeNode;
delete temp;
return false;
}
catch (std::bad_alloc)
{
return true;
}
}
//Insert node into the tree in the right place
template<class ItemType>
void BST_312 <ItemType>::insertItem(TreeNode*& t, const ItemType& newItem)
{
if (t == NULL) //if there's nothing in the tree yet
{
TreeNode* temp = new TreeNode; //allocate memory for the new node
temp->data = newItem;
temp->left = NULL;
temp->right = NULL;
t = temp; //the root of the tree is now the new node that was inserted
}
else if (newItem > t->data) //if data is greater than the current node, place it to the right
{
insertItem(t->right, newItem);
}
else if (newItem < t->data) //if data is less than the current node, place it to the left
{
insertItem(t->left, newItem);
}
}
template<class ItemType>
void BST_312 <ItemType>::insertItem(const ItemType& newItem)
{
insertItem(root, newItem);
}
//Count all nodes in the BST
template<class ItemType>
int BST_312 <ItemType>::countNodes(TreeNode* t) const
{
int count = 0;
if(t != NULL)
{
count++;
count = count + countNodes(t->left);
count = count + countNodes(t->right);
}
return count;
}
template<class ItemType>
int BST_312 <ItemType>::countNodes()
{
countNodes(root);
}
//Node, Left, Right
template<class ItemType>
void BST_312 <ItemType>::preOrderTraversal(TreeNode* t,vector<ItemType>& result) const
{
if (t != NULL)
{
result.push_back(t->data);
preOrderTraversal(t->left, result);
preOrderTraversal(t->right, result);
}
}
template<class ItemType>
vector<ItemType> BST_312 <ItemType>::preOrderTraversal()
{
vector<ItemType> pre_result; //make vector to return the BST in preorder
pre_result.resize(0);
preOrderTraversal(root, pre_result);
return pre_result;
}
//Left, Node, Right
template<class ItemType>
void BST_312 <ItemType>::inOrderTraversal(TreeNode* t,vector<ItemType>& result) const
{
if (t != NULL)
{
inOrderTraversal(t->left, result);
result.push_back(t->data);
inOrderTraversal(t->right, result);
}
}
template<class ItemType>
vector<ItemType> BST_312 <ItemType>::inOrderTraversal()
{
vector<ItemType> in_result; //make vector to return the BST inorder
in_result.resize(0);
inOrderTraversal(root, in_result);
return in_result;
}
//Left, Right, Node
template<class ItemType>
void BST_312 <ItemType>::postOrderTraversal(TreeNode* t,vector<ItemType>& result) const
{
if (t != NULL)
{
postOrderTraversal(t->left, result);
postOrderTraversal(t->right, result);
result.push_back(t->data);
}
}
template<class ItemType>
vector<ItemType> BST_312 <ItemType>::postOrderTraversal()
{
vector<ItemType> post_result; //make vector to return the BST in postorder
post_result.resize(0);
postOrderTraversal(root, post_result);
return post_result;
}
//See if the given item is in the tree
template<class ItemType>
bool BST_312 <ItemType>::isItemInTree(const ItemType& item)
{
TreeNode* temp = root; //make a temporary node that starts at the root
while (temp != NULL)
{
if (temp->data == item) //check if the data in the node is equal to the item
{
return true; //found match!
}
else if (temp->data < item) //if the item is greater than the data at the current node, go right to find it
{
temp = temp->right;
}
else if (temp->data > item) //if the item is less than the data at the current node, go left to find it
{
temp = temp->left;
}
}
return false; //if temp (root) is NULL then there's nothing in the tree
}
#endif