Using Namespace
Using Namespace
cpp
2 //BST Implimentation file
3
4 // #include "actorList.h"
5 #include "binarySearchTree.h"
6 #include <iostream>
7 using namespace std;
8
9 // DeleteBST()
10 // Delete an entire BST. All memory is released
11 // using a "PostOrder" traversal method.
12 void binarySearchTree::deleteTree( binaryNodePtr& treePtr ){
13 if ( treePtr != NULL ) {
14 deleteTree( treePtr -> left );
15 deleteTree( treePtr -> right );
16 delete treePtr;
17 treePtr = NULL;
18 }
19 }
20
21 //deletes a node of the given title from the tree
22 void binarySearchTree::deleteNode( binaryNodePtr& t, string val ){
23 if( t == NULL ) {
24 return;
25 } else if ( val == t -> data.title ) {
26 deleteNodeItem( t );
27 } else if ( val < t -> data.title ) {
28 deleteNode( t -> left, val );
29 } else {
30 deleteNode( t -> right, val );
31 }
32 }
33
34 //deletes the given node from the tree
35 void binarySearchTree::deleteNodeItem( binaryNodePtr& t ){
36
37 binaryNodePtr delPtr;
38 if( isLeaf(t) ) { //if the node is a leaf, just delete it
39 delete t;
40 t = NULL;
41 } else if ( t -> left == NULL ) { //if node left is empty,
42 delPtr = t;
43 t = t -> right;
44 delPtr -> right = NULL;
45 delete delPtr; //replace it with the right node/subtree
46 } else if ( t -> right == NULL ) { //if node right is empty,
47 delPtr = t;
48 t = t -> left;
49 delPtr -> left = NULL;
50 delete delPtr; //replace it with the left node/subtree
51 } else { //if both left and right are full,
52 DATA_TYPE replacementItem;
53 processLeftMost( t -> right, replacementItem );
54 t -> data = replacementItem;
55 }
56
57 }
58
59 //used to replace a child when its parent is deleted
60 void binarySearchTree::processLeftMost( binaryNodePtr& t, DATA_TYPE& theItem ){
61 if( t -> left != NULL ){
62 processLeftMost( t->left, theItem );
63 } else {
64 theItem = t -> data;
65 binaryNodePtr delPtr = t;
66 t = t -> right;
67 delPtr -> right = NULL;
68 delete delPtr;
69 }
70 }
71
72 //returns true if a node is a leaf
73 bool binarySearchTree::isLeaf( binaryNodePtr n ){
74 if ( n -> left == NULL && n -> right == NULL ){
75 return true;
76 } else {
77 return false;
78 }
79 }
80
81 // SearchNodeInBST()
82 // Find a given node by "key" in BST. If successful, it
83 // returns the pointer that points to the node with "key";
84 // otherwise, it returns NULL. It uses preorder traversal.
85 binaryNodePtr binarySearchTree::searchNodeInBST( binaryNodePtr treePtr, string key ){
86 // cout << "Searching for node " << key << " in BST" << endl;
87 if( treePtr != NULL ) {
88 if( key == treePtr -> data.title ){
89 // cout << "Found!" << endl;
90 return treePtr;
91 } else if( key < treePtr -> data.title ){
92 // Search for "key" in left subtree
93 return searchNodeInBST( treePtr -> left, key );
94 } else { // (key > tree_ptr->data)
95 // Search for "key" in right subtree
96 return searchNodeInBST( treePtr -> right, key );
97 }
98 } else {
99 return NULL;
100 }
101 }
102
103 //Prints tree preOrder
104 //root should be printed first
105 void binarySearchTree::printPreOrder( binaryNodePtr t ){
106 if ( t != NULL ){
107 cout << t -> data.title << endl;
108 printPreOrder( t -> left);
109 printPreOrder( t -> right);
110 }
111 }
112
113 //prints tree in order
114 //root should come somwhere in the middle
115 void binarySearchTree::printInOrder( binaryNodePtr t ){
116 if ( t != NULL ){
117 printInOrder( t -> left );
118 cout << t -> data.title << endl;
119 printInOrder( t -> right );
120 }
121 }
122
123 //prints tree post order
124 //root should come at the end
125 void binarySearchTree::printPostOrder( binaryNodePtr t ){
126 if ( t != NULL ){
127 printPostOrder( t -> left );
128 printPostOrder( t -> right );
129 cout << t -> data.title << endl;
130 }
131 }
132
133 //prints all data in a node
134 void binarySearchTree::printNode( binaryNodePtr p ){
135 // cout << "Printing Node" << endl;
136 if ( p != NULL ){
137 cout << "-------------------" << endl;
138 cout << "Title: " << p -> data.title << endl;
139 cout << "Category: " << p -> data.category << endl;
140 cout << "URL: " << p -> data.url << endl;
141 cout << "Start Year: " << p -> data.yearStart << endl;
142 cout << "End Year: " << p -> data.yearEnd << endl;
143 cout << "Actors: " << endl;
144 p -> data.actors.printList();
145 cout << "-------------------" << endl;
146 } else {
147 cout << "The show does not exist in the tree" << endl;
148 }
149 }
150
151 void binarySearchTree::printShowsInRange( binaryNodePtr p, int start, int end ){
152 if ( p != NULL ){
153 printShowsInRange( p -> left, start, end );
154 if ( p -> data.yearStart <= end && p -> data.yearStart >= start ) {
155 cout << p -> data.title << " (" << p -> data.yearStart << "-";
156 cout << p -> data.yearEnd << ")" << endl;
157 }
158 printShowsInRange( p -> right, start, end );
159 }
160 }
161
162 void binarySearchTree::printShowsWithActor( binaryNodePtr p, string actor ){
163 if ( p != NULL ){
164 printShowsWithActor( p -> left, actor );
165 if ( p -> data.actors.containsValue( actor ) ) {
166 cout << p -> data.title << endl;
167 }
168 printShowsWithActor( p -> right, actor );
169 }
170 }
171
172 //returns true if tree is empty
173 bool binarySearchTree::isEmpty(){
174 if ( root == NULL ){
175 return true;
176 } else {
177 return false;
178 }
179 }
180
181 // AddNode()
182 // Add (insert) new item into the BST, whose
183 // root node is pointed to by "rootPtr". If
184 // the data already exists, it is ignored.
185 void binarySearchTree::addNode( DATA_TYPE newData ){
186
187 //create new node to hold data
188 binaryNodePtr newNode = new binaryNode;
189 //initilize node with new data
190 newNode -> data = newData;
191 newNode -> left = NULL;
192 newNode -> right = NULL;
193 //if node is empty, insert newNode as the root
194 if ( root == NULL ){
195 root = newNode;
196 // cout << "Making" << newData << " into the root" << endl;
197 } else { //otherwise, try to find suitable insertion location
198
199 binaryNodePtr p = root;
200 binaryNodePtr targetNode;
201
202 while ( p != NULL ){
203 targetNode = p;
204 if ( newData.title == p -> data.title ){
205 //attempting to insert repeat data, ignore
206 // cout << "Value already in the tree" << endl;
207 return;
208 } else if( newData.title < p -> data.title ) {
209 //Search left subtree for insertion location
210 // cout << "Checking left subtree" << endl;
211 p = p -> left;
212 } else {
213 //Search right subtree for insertion location
214 // cout << "Checking right subtree" << endl;
215 p = p -> right;
216 }
217 }
218 // "targetNodePtr" is the pointer to the
219 // parent of the new node. Decide where
220 // it will be inserted.
221 if( newData.title < targetNode -> data.title ){
222 targetNode -> left = newNode;
223 // cout << "Inserting to the left" << endl;
224 } else {// insert it as its right child
225 targetNode -> right = newNode;
226 // cout << "Inserting to the right" << endl;
227 }
228
229 }
230 }
231
232 //Prints whether or not a node of the given title is found within the tree
233 void binarySearchTree::searchNode( string searchKey ){
234 binaryNodePtr searchPtr = NULL;
235 searchPtr = searchNodeInBST( root, searchKey );
236 if ( searchPtr != NULL ) {
237 cout << "\n Node: " << searchPtr -> data.title << " found in the BST" << endl;
238 } else {
239 cout << "\n Node: " << searchKey << " not found" << endl;
240 }
241 }
242
243 //removes a node of the given title from the tree
244 void binarySearchTree::deleteNode( string t ){
245 binaryNodePtr p = searchNodeInBST( root, t );
246 deleteNode( root, t );
247 }
248
249 //prints the data (actors, etc) from a specific show in the tree
250 //suing searchNodeInBST and printNode functions
251 void binarySearchTree::printShow( string showName ){
252 // cout << "printing show" << endl;
253 binaryNodePtr p = searchNodeInBST( root, showName );
254 if ( p != NULL ){
255 printNode( p );
256 } else {
257 cout << "Show " << showName << " cannot be found" << endl;
258 }
259
260 }
261
262 void binarySearchTree::printShowsInRange( int start, int end ){
263 printShowsInRange( root, start, end );
264 }
265
266 void binarySearchTree::printShowsWithActor( string actor ){
267 printShowsWithActor( root, actor );
268 }
269
270
271
272
273
274