1
+ /* *************************************************************************
2
+ * Joel Brigida
3
+ * COP3530: Data Structures
4
+ * This is a CLASS implementation of the Binary Search Tree ADT.
5
+ * * Basic operations:
6
+ * Constructor: Constructs an empty BST
7
+ * empty: Checks if a BST is empty
8
+ * search: Search a BST for an item
9
+ * insert: Inserts a value into a BST
10
+ * remove: Removes a value from a BST
11
+ * inorder: Inorder traversal of a BST -- output the data values
12
+ * graph: Output a grapical representation of a BST
13
+ * * Private utility helper operations:
14
+ * search2: Used by delete
15
+ * inorderAux: Used by inorder
16
+ * graphAux: Used by graph
17
+ * * Other operations:
18
+ * destructor
19
+ * copy constructor
20
+ * Overloaded assignment operator
21
+ * preorder, postorder, level-by-level traversals & level finder.
22
+ * Note: Execution terminates if memory isn't available for a new BST node.
23
+ **************************************************************************/
24
+ #ifndef BINARY_SEARCH_TREE
25
+ #define BINARY_SEARCH_TREE
26
+
27
+ #include < iostream>
28
+ #include < iomanip>
29
+
30
+ using namespace std ;
31
+
32
+ template <typename DataType>
33
+ class BST
34
+ {
35
+ public:
36
+
37
+ BST ();
38
+ bool empty () const ;
39
+ bool search (const DataType& item) const ;
40
+ void insert (const DataType& item);
41
+ void remove (const DataType& item);
42
+ void inorder (ostream& out) const ;
43
+ void graph (ostream& out) const ;
44
+
45
+ private:
46
+
47
+ class BinNode // Nested Private Node Class
48
+ {
49
+ public:
50
+
51
+ DataType data;
52
+ BinNode *left; // Pointer To Left Child Node
53
+ BinNode *right; // Pointer To Right Child Node
54
+ BinNode () : left(0 ), right(0 ) {} // Default Constructor for Empty BinNode.
55
+ BinNode (DataType item) : data(item), left(0 ), right(0 ) {} // Explicit Value Constructor: Inserts 'item'
56
+
57
+ }; // end of class BinNode declaration
58
+
59
+ typedef BinNode *BinNodePointer;
60
+ BinNodePointer myRoot; // Points to the Root Node
61
+ // Private Helper Functions:
62
+ void search2 (const DataType& item, bool & found, BinNodePointer& locptr, BinNodePointer& parent) const ;
63
+ void inorderAux (ostream& out, BST<DataType>::BinNodePointer subtreePtr) const ;
64
+ void graphAux (ostream& out, int indent, BST<DataType>::BinNodePointer subtreeRoot) const ;
65
+
66
+ }; // end of class template declaration
67
+
68
+ /* *****************************************************************
69
+ Function Name: BST()
70
+ Description: Default Constructor for BST Object. Constructs an
71
+ empty Binary Search Tree with no nodes.
72
+ Precondition: None.
73
+ Postcondition: An empty BST has been constructed.
74
+ ******************************************************************/
75
+ template <typename DataType>
76
+ inline BST<DataType>::BST() : myRoot(0 ) {}
77
+
78
+ /* *****************************************************************
79
+ Function Name: empty()
80
+ Description: Checks if BST is empty.
81
+ Precondition: None.
82
+ Postcondition: Returns true if BST is empty and false otherwise.
83
+ ******************************************************************/
84
+ template <typename DataType>
85
+ inline bool BST<DataType>::empty() const
86
+ {
87
+ return myRoot == 0 ;
88
+ }
89
+
90
+ /* ******************************************************************
91
+ Function Name: search()
92
+ Description: Searches the BST for item.
93
+ Precondition: None.
94
+ Postcondition: Returns true if item found, and false otherwise.
95
+ ******************************************************************/
96
+ template <typename DataType>
97
+ bool BST<DataType>::search(const DataType& item) const
98
+ {
99
+ BST<DataType>::BinNodePointer locptr = myRoot;
100
+ bool found = false ;
101
+
102
+ while (!found && locptr != 0 )
103
+ {
104
+ if (item < locptr->data ) // descend left
105
+ {
106
+ locptr = locptr->left ;
107
+ }
108
+ else if (locptr->data < item) // descend right
109
+ {
110
+ locptr = locptr->right ;
111
+ }
112
+ else // item found
113
+ {
114
+ found = true ;
115
+ }
116
+ }
117
+ return found;
118
+ }
119
+
120
+ /* *****************************************************************
121
+ Function Name: insert()
122
+ Desription: Inserts item into BST.
123
+ Precondition: None.
124
+ Postcondition: BST has been modified with item inserted at proper
125
+ position to maintain BST property.
126
+ ******************************************************************/
127
+ template <typename DataType>
128
+ inline void BST<DataType>::insert(const DataType& item)
129
+ {
130
+ BST<DataType>::BinNodePointer
131
+ locptr = myRoot, // search pointer
132
+ parent = 0 ; // pointer to parent of current node
133
+
134
+ bool found = false ; // indicates if item already in BST
135
+ while (!found && locptr != 0 )
136
+ {
137
+ parent = locptr;
138
+
139
+ if (item < locptr->data ) // descend left
140
+ {
141
+ locptr = locptr->left ;
142
+ }
143
+ else if (locptr->data < item) // descend right
144
+ {
145
+ locptr = locptr->right ;
146
+ }
147
+ else // item found
148
+ {
149
+ found = true ;
150
+ }
151
+ }
152
+ if (!found)
153
+ { // construct node containing item
154
+ locptr = new BST<DataType>::BinNode (item);
155
+
156
+ if (parent == 0 ) // empty tree
157
+ {
158
+ myRoot = locptr;
159
+ }
160
+ else if (item < parent->data ) // insert to left of parent
161
+ {
162
+ parent->left = locptr;
163
+ }
164
+ else // insert to right of parent
165
+ {
166
+ parent->right = locptr;
167
+ }
168
+ }
169
+ else
170
+ {
171
+ cout << " Item already in the tree" << endl;
172
+ }
173
+ }
174
+
175
+ /* *****************************************************************
176
+ Function Name: remove()
177
+ Description: Remove item from BST.
178
+ Precondition: None.
179
+ Postcondition: BST has been modified with item removed (if present)
180
+ and BST property is maintained.
181
+ Note: remove uses auxiliary function search2() to locate the node
182
+ containing item and its parent.
183
+ ******************************************************************/
184
+ template <typename DataType>
185
+ void BST<DataType>::remove(const DataType& item)
186
+ {
187
+
188
+ bool found; // signals if item is found
189
+ BST<DataType>::BinNodePointer
190
+ x, // points to node containing
191
+ parent; // " " parent of x and xSucc
192
+
193
+ search2 (item, found, x, parent);
194
+
195
+ if (!found)
196
+ {
197
+ cout << " Item not in the BST" << endl;
198
+ return ;
199
+ }
200
+ // else
201
+ if (x->left != 0 && x->right != 0 ) // if node has 2 children
202
+ {
203
+ BST<DataType>::BinNodePointer xSucc = x->right ; // Find x's inorder successor and its parent
204
+ parent = x;
205
+
206
+ while (xSucc->left != 0 ) // descend left
207
+ {
208
+ parent = xSucc;
209
+ xSucc = xSucc->left ;
210
+ }
211
+
212
+ x->data = xSucc->data ; // Move contents of xSucc to x and change x
213
+ x = xSucc; // to point to successor, which will be removed.
214
+ } // end if node has 2 children
215
+
216
+ // Now proceed with case where node has 0 or 2 child
217
+ BST<DataType>::BinNodePointer subtree = x->left ; // pointer to a subtree of x
218
+ if (subtree == 0 )
219
+ {
220
+ subtree = x->right ;
221
+ }
222
+ if (parent == 0 ) // root being removed
223
+ {
224
+ myRoot = subtree;
225
+ }
226
+ else if (parent->left == x) // left child of parent
227
+ {
228
+ parent->left = subtree;
229
+ }
230
+ else // right child of parent
231
+ {
232
+ parent->right = subtree;
233
+ }
234
+ delete x;
235
+ }
236
+
237
+ /* *****************************************************************
238
+ Function Name: inorder()
239
+ Description: Performs an Inorder traversal of BST.
240
+ Precondition: ostream out is open.
241
+ Postcondition: BST has been inorder traversed and values in nodes
242
+ have been output to out stream.
243
+ Note: inorder uses private auxiliary function inorderAux().
244
+ ******************************************************************/
245
+ template <typename DataType>
246
+ inline void BST<DataType>::inorder(ostream& out) const
247
+ {
248
+ inorderAux (out, myRoot);
249
+ }
250
+
251
+ /* *****************************************************************
252
+ Function Name: graph()
253
+ Description: Graphic output of BST.
254
+ Precondition: ostream out is open.
255
+ Postcondition: Graphical representation of BST has been output to out.
256
+ Note: graph() uses private auxiliary function graphAux().
257
+ ******************************************************************/
258
+ template <typename DataType>
259
+ inline void BST<DataType>::graph(ostream& out) const
260
+ {
261
+ graphAux (out, 0 , myRoot);
262
+ }
263
+
264
+ /* *****************************************************************
265
+ Function Name: search2()
266
+ Description: Private Helper Function: Locates a node containing
267
+ 'item' and its parent.
268
+ Precondition: None.
269
+ Postcondition: locptr points to node containing item or is null if
270
+ not found, and parent points to its parent.
271
+ ******************************************************************/
272
+ template <typename DataType>
273
+ void BST<DataType>::search2(const DataType& item, bool & found, BST<DataType>::BinNodePointer& locptr, BST<DataType>::BinNodePointer& parent) const
274
+ {
275
+ locptr = myRoot;
276
+ parent = 0 ;
277
+ found = false ;
278
+
279
+ while (!found && locptr != 0 )
280
+ {
281
+ if (item < locptr->data ) // descend left
282
+ {
283
+ parent = locptr;
284
+ locptr = locptr->left ;
285
+ }
286
+ else if (locptr->data < item) // descend right
287
+ {
288
+ parent = locptr;
289
+ locptr = locptr->right ;
290
+ }
291
+ else // item found
292
+ {
293
+ found = true ;
294
+ }
295
+ }
296
+ }
297
+
298
+ /* ******************************************************************
299
+ Function Name: inorderAux()
300
+ Description: Private Helper Function: Performes Inorder traversal
301
+ of BST.
302
+ Precondition: ostream out is open and subtreePtr points to a subtree
303
+ of this BST.
304
+ Postcondition: Subtree with root pointed to by subtreePtr has been
305
+ output to out.
306
+ *******************************************************************/
307
+ template <typename DataType>
308
+ void BST<DataType>::inorderAux(ostream& out, BST<DataType>::BinNodePointer subtreeRoot) const
309
+ {
310
+ if (subtreeRoot != 0 )
311
+ {
312
+ inorderAux (out, subtreeRoot->left ); // L operation
313
+ out << subtreeRoot->data << " " ; // V operation
314
+ inorderAux (out, subtreeRoot->right ); // R operation
315
+ }
316
+ }
317
+
318
+ /* ******************************************************************
319
+ Function Name: graphAux()
320
+ Description: Private Helper Function: Graphs auxiliary function.
321
+ Precondition: ostream out is open & subtreePtr points to a subtree
322
+ of this BST.
323
+ Postcondition: Graphical representation of subtree with root pointed
324
+ to by subtreePtr has been output to out, indented indent spaces.
325
+ *******************************************************************/
326
+ template <typename DataType>
327
+ void BST<DataType>::graphAux(ostream& out, int indent, BST<DataType>::BinNodePointer subtreeRoot) const
328
+ {
329
+ if (subtreeRoot != 0 )
330
+ {
331
+ graphAux (out, indent + 8 , subtreeRoot->right );
332
+ out << setw (indent) << " " << subtreeRoot->data << endl;
333
+ graphAux (out, indent + 8 , subtreeRoot->left );
334
+ }
335
+ }
336
+
337
+ #endif
0 commit comments