-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.cpp
More file actions
254 lines (201 loc) · 7.29 KB
/
Copy pathBST.cpp
File metadata and controls
254 lines (201 loc) · 7.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
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
/*
* File: BST.cpp
* Author: kim
*
* Created on November 9, 2014, 6:01 PM
*/
#include "BST.h"
#include <cstddef> // definition of NULL
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <queue> // for breadth first traversal
using namespace std;
bool BST::isEmpty() const {
return (root == NULL);
// returns true if root is NULL, false if root is not NULL
}
void BST::searchTree(const int searchKey) {
search(root, searchKey); // start with root as tree pointer
}
// Recursively searches for an frequency in BST
void BST::search(TreeNode*& treePtr, const int& searchKey) {
if (treePtr == NULL) // if the whole BST has been searched aka treePtr is NULL
// and there are no matches
{
cout << "Data not found" << endl;
} else if (searchKey == treePtr->frequency) // if searchKey is the same as the frequency
// pointed to by the pointer
{
cout << "The data is found" << endl;
} else if (searchKey < treePtr->frequency) {
search(treePtr->leftChildPtr, searchKey);
} else // search the right subtree
{
search(treePtr->rightChildPtr, searchKey);
}
}
void BST::searchTreeInsert(const int newFrequency, const string newWord) {
insertItem(root, newFrequency, newWord); // start with root as tree pointer
}
// Recursively inserts an frequency into a BST
void BST::insertItem(TreeNode*& treePtr, const int& newFrequency,
const string& newWord) {
if (treePtr == NULL) // position of insertion is found; insert after leaf
{
treePtr = new TreeNode(newFrequency, NULL, NULL);
treePtr->word = newWord;
}// else search for the insertion position
else if (newFrequency < treePtr->frequency) {
insertItem(treePtr->leftChildPtr, newFrequency, newWord);
} else // search the right subtree
{
insertItem(treePtr->rightChildPtr, newFrequency, newWord);
}
}// end insertItem
void BST::searchTreeDelete(int searchKey) {
deleteItem(root, searchKey); // start with root
}
// Recursively deletes an frequency from a BST
void BST::deleteItem(TreeNode*& treePtr, int searchKey) {
if (treePtr == NULL) {
cout << "delete failed, empty tree" << endl;
} else if (searchKey == treePtr->frequency) {
// frequency is found, frequency is in the root of some subtree
deleteNodeItem(treePtr); // delete frequency
}// else search for the frequency
else if (searchKey < treePtr->frequency) {
// search left subtree
deleteItem(treePtr->leftChildPtr, searchKey);
} else {
// search right subtree
deleteItem(treePtr->rightChildPtr, searchKey);
}
} // end deleteItem
// Deletes the frequency in the root of a given tree
// There are four cases:
// 1. The root is a leaf
// 2. The root has a right child
// 3. The root has a left child
// 4. The root has two children
void BST::deleteNodeItem(TreeNode*& nodePtr) {
TreeNode *delPtr;
int replacementItem;
// test for a leaf
if ((nodePtr->leftChildPtr == NULL) && (nodePtr->rightChildPtr == NULL)) {
delete nodePtr; // free the memory
nodePtr = NULL; // make nodePtr point to NULL
}// end if leaf
// test for right child
else if (nodePtr->leftChildPtr == NULL) {
delPtr = nodePtr;
nodePtr = nodePtr->rightChildPtr; // replacing frequency to be deleted
// with right child
delPtr->rightChildPtr = NULL; // deleting the frequency
delete delPtr; // delete the temp pointer
}// end if right child
// test for left child
else if (nodePtr->rightChildPtr == NULL) {
delPtr = nodePtr;
nodePtr = nodePtr->leftChildPtr;
delPtr->leftChildPtr = NULL;
delete delPtr;
}// end if left child
// there are two children
// retrieve and delete the inorder successor
else {
processLeftmost(nodePtr->rightChildPtr, replacementItem);
nodePtr->frequency = replacementItem;
} // end if two children
} // end deleteNodeItem
void BST::processLeftmost(TreeNode*& nodePtr, int& treeItem) {
// if leftmost found
if (nodePtr->leftChildPtr == NULL) {
treeItem = nodePtr->frequency; // copying frequency to a temp
TreeNode* delPtr = nodePtr; // make a temp delPtr and point to frequency to be deleted
nodePtr = nodePtr->rightChildPtr;
delPtr->rightChildPtr = NULL;
delete delPtr;
}// else find leftmost
else {
processLeftmost(nodePtr->leftChildPtr, treeItem);
}
} // end processLeftmost
// Traverse functions
void BST::preorderTraverse() {
preorder(root);
}
void BST::preorder(TreeNode* treePtr) {
if (treePtr != NULL) {
cout << "word: " << treePtr->word << endl;
cout << "frequency: " << treePtr->frequency << endl;
cout << endl;
preorder(treePtr->leftChildPtr);
preorder(treePtr->rightChildPtr);
} // end if
} // end preorder
void BST::inorderTraverse() {
inorder(root);
}
void BST::inorder(TreeNode* treePtr) {
if (treePtr != NULL) {
inorder(treePtr->leftChildPtr);
cout << "word: " << treePtr->word << endl;
cout << "frequency: " << treePtr->frequency << endl;
cout << endl;
inorder(treePtr->rightChildPtr);
} // end if
} // end inorder
void BST::inorderTraversewithHeight() {
inorder(root, 0);
}
void BST::inorder(TreeNode* treePtr, int height) {
if (treePtr != NULL) {
inorder(treePtr->leftChildPtr, height+1);
// cout << "indent by: " << height <<endl;
for (int i = 0; i < height; i++) cout << " ";
cout << treePtr->word << "(" << treePtr->frequency << ")" <<endl;
// cout << "word: " << treePtr->word << endl;
// cout << "frequency: " << treePtr->frequency << endl;
// cout << endl;
inorder(treePtr->rightChildPtr, height+1);
} // end if
} // end inorder
void BST::postorderTraverse() {
postorder(root);
}
void BST::postorder(TreeNode* treePtr) {
if (treePtr != NULL) {
postorder(treePtr->leftChildPtr);
postorder(treePtr->rightChildPtr);
cout << "word: " << treePtr->word << endl;
cout << "frequency: " << treePtr->frequency << endl;
cout << endl;
} // end if
} // end postorder
void BST::breadthfirstTraverse() {
breadthfirst(root);
}
void BST::breadthfirst(TreeNode* treePtr) {
queue<TreeNode*> queue; // temp queue holding tree node pointers
TreeNode* traverse; // pointer to node being processed
if (treePtr == NULL)
return; // nothing to traverse
queue.push(treePtr); // put something in queue intially, so that we enter
// the body of the loop
while (!queue.empty()) {
traverse = queue.front(); // Point to first item enqueued
queue.pop(); // Pop out first item enqueued
// Visit the node pointed to by traverse.
cout << "Word: " << traverse->word << endl;
cout << "Frequency: " << traverse->frequency << endl;
cout << endl;
// If there is a left child, add it for later processing
if (traverse->leftChildPtr != NULL)
queue.push(traverse->leftChildPtr);
// If there is a right child, add it for later processing
if (traverse->rightChildPtr != NULL)
queue.push(traverse->rightChildPtr);
} // end while
}