|
| 1 | +#include <iostream> |
| 2 | +#include <fstream> |
| 3 | +#include <string> |
| 4 | +#include <cctype> |
| 5 | +#include <cstring> |
| 6 | + |
| 7 | +#include "TreeType.h" |
| 8 | +#include "tools.hpp" |
| 9 | + |
| 10 | +using namespace std; |
| 11 | + |
| 12 | +void MakeTree(TreeType& tree, int info[],int toIndex,int fromIndex); |
| 13 | +void AddElements(TreeType &tree, int info[], int toIndex, int fromIndex); |
| 14 | +int Count(TreeType& tree, ItemType value); |
| 15 | + |
| 16 | +int menuSelection(void); |
| 17 | + |
| 18 | +int main() |
| 19 | +{ |
| 20 | + banner(); // Call to banner |
| 21 | + |
| 22 | + int item; |
| 23 | + TreeType tree; |
| 24 | + bool found; |
| 25 | + int * info; |
| 26 | + |
| 27 | + int select; |
| 28 | + string option; |
| 29 | + |
| 30 | + ofstream outFile; // file containing output |
| 31 | + string outFileName = "out"; |
| 32 | + string outputLabel = "Tree"; |
| 33 | + outFile.open(outFileName.c_str(), ios::out | ios::app); |
| 34 | + outFile << "\n[" << outputLabel << "]" << endl << endl; |
| 35 | + |
| 36 | + do{ |
| 37 | + select = menuSelection(); |
| 38 | + switch(select) |
| 39 | + { |
| 40 | + // Case to run the ancestor function (iterative) |
| 41 | + case 1: |
| 42 | + do{ |
| 43 | + cout << "Input the element whose ancestors are to be displayed: " << endl; |
| 44 | + // Get the node for which ancestor is to be printed |
| 45 | + cin >> item; |
| 46 | + |
| 47 | + tree.GetItem(item, found); |
| 48 | + |
| 49 | + if (found) |
| 50 | + { |
| 51 | + outFile << "The ancestors for " << item << " are: " << endl; |
| 52 | + cout << "The ancestors for " << item << " are: " << endl; |
| 53 | + // Function call to ancestor |
| 54 | + tree.Ancestors(item, found, outFile); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + outFile << "Element not present in the tree." << endl; |
| 59 | + cout << "Element not present in the tree." << endl; |
| 60 | + } |
| 61 | + |
| 62 | + cout << "\n -> Do you want to print ancestors of another element? [Y/N]"; |
| 63 | + getline(cin>>ws, option); |
| 64 | + |
| 65 | + }while(option == "Y" || option == "y" || option == "Yes"); |
| 66 | + break; |
| 67 | + // Case to insert elements |
| 68 | + case 2: |
| 69 | + do{ |
| 70 | + cout << "Please enter the element to be inserted: " << endl; |
| 71 | + cin >> item; |
| 72 | + // Function call to putitem |
| 73 | + tree.PutItem(item); |
| 74 | + outFile << item << " is inserted" << endl; |
| 75 | + cout << item << " is inserted" << endl; |
| 76 | + |
| 77 | + cout <<"\n -> Do you want to insert another element? [Y/N]"; |
| 78 | + getline(cin>>ws, option); |
| 79 | + |
| 80 | + }while(option == "Y" || option == "y" || option == "Yes"); |
| 81 | + break; |
| 82 | + // Case to delete elements |
| 83 | + case 3: |
| 84 | + do{ |
| 85 | + cout << "Please enter the element to be deleted: " << endl; |
| 86 | + cin >> item; |
| 87 | + // Function call to deleteitem |
| 88 | + tree.DeleteItem(item); |
| 89 | + outFile << item << " is deleted" << endl; |
| 90 | + cout << item << " is deleted" << endl; |
| 91 | + |
| 92 | + cout <<"\n -> Do you want to delete another element? [Y/N]"; |
| 93 | + getline(cin>>ws, option); |
| 94 | + |
| 95 | + }while(option == "Y" || option == "y" || option == "Yes"); |
| 96 | + break; |
| 97 | + // Case to run the ancestor function recursively and print the ancestors |
| 98 | + case 4: |
| 99 | + do{ |
| 100 | + cout << "Input the element whose ancestors are to be displayed: " << endl; |
| 101 | + cin >> item; |
| 102 | + // Function call to getitem |
| 103 | + tree.GetItem(item, found); |
| 104 | + |
| 105 | + if (found) |
| 106 | + { |
| 107 | + outFile << "The ancestors for " << item << " are: " << endl; |
| 108 | + cout << "The ancestors for " << item << " are: " << endl; |
| 109 | + // Function call to Ancestors function to print in reverse |
| 110 | + tree.AncestorsReverse(item, found, outFile); |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + outFile << "Element not present in the tree." << endl; |
| 115 | + cout << "Element not present in the tree." << endl; |
| 116 | + } |
| 117 | + |
| 118 | + cout << "\n -> Do you want to print ancestors of another element? [Y/N]"; |
| 119 | + getline(cin>>ws, option); |
| 120 | + |
| 121 | + }while(option == "Y" || option == "y" || option == "Yes"); |
| 122 | + break; |
| 123 | + // Case to run the Make tree function |
| 124 | + case 5: |
| 125 | + { |
| 126 | + int length; |
| 127 | + cout << "Enter the number of elements you want to insert: " << endl; |
| 128 | + cin >> length; |
| 129 | + info = new int[length]; |
| 130 | + |
| 131 | + for(int j = 0; j < length; j++) |
| 132 | + { |
| 133 | + cout << "Insert element: " << endl; |
| 134 | + cin >> info[j]; |
| 135 | + } |
| 136 | + length--; |
| 137 | + // Function call to Maketree function |
| 138 | + MakeTree(tree, info, length, 0); |
| 139 | + } |
| 140 | + break; |
| 141 | + // Case to print tree elements |
| 142 | + case 6: |
| 143 | + do{ |
| 144 | + // function call to print function |
| 145 | + tree.Print(outFile); |
| 146 | + |
| 147 | + cout <<"\n -> Do you want to print again? [Y/N]"; |
| 148 | + getline(cin>>ws, option); |
| 149 | + |
| 150 | + }while(option == "Y" || option == "y" || option == "Yes"); |
| 151 | + break; |
| 152 | + //Case to print the leaf nodes of the tree |
| 153 | + case 7: |
| 154 | + do{ |
| 155 | + int leaves = 0; |
| 156 | + // function call to leafcount function |
| 157 | + leaves = tree.LeafCount(leaves); |
| 158 | + |
| 159 | + outFile << "Leaf count of tree is " << leaves << endl; |
| 160 | + cout << "Leaf count of tree is " << leaves << endl; |
| 161 | + |
| 162 | + cout <<"\n -> Do you want to print again? [Y/N]"; |
| 163 | + getline(cin>>ws, option); |
| 164 | + |
| 165 | + }while(option == "Y" || option == "y" || option == "Yes"); |
| 166 | + break; |
| 167 | + // Case to run function to print the single parents in a tree |
| 168 | + case 8: |
| 169 | + do{ |
| 170 | + int spcount = 0; |
| 171 | + // function call to single parent count function |
| 172 | + spcount = tree.SingleParentCount(spcount); |
| 173 | + |
| 174 | + outFile << "Single Parent count of tree is " << spcount << endl; |
| 175 | + cout << "Single Parent count of tree is " << spcount << endl; |
| 176 | + |
| 177 | + cout <<"\n -> Do you want to print again? [Y/N]"; |
| 178 | + getline(cin>>ws, option); |
| 179 | + |
| 180 | + }while(option == "Y" || option == "y" || option == "Yes"); |
| 181 | + break; |
| 182 | + case 9: |
| 183 | + do{ |
| 184 | + int value; |
| 185 | + |
| 186 | + cout << "Enter a value:" << endl; |
| 187 | + cin >> value; |
| 188 | + // function call to count function |
| 189 | + value = Count(tree, value); |
| 190 | + |
| 191 | + outFile << "Node count of values less than parameter is " << value << endl; |
| 192 | + cout << "Node count of values less than parameter is " << value << endl; |
| 193 | + |
| 194 | + cout <<"\n -> Do you want to print again? [Y/N]"; |
| 195 | + getline(cin>>ws, option); |
| 196 | + |
| 197 | + }while(option == "Y" || option == "y" || option == "Yes"); |
| 198 | + break; |
| 199 | + // Case to display a message and quit the program. |
| 200 | + case 10: |
| 201 | + cout<<"\nGood Bye!!!"<<endl; |
| 202 | + break; |
| 203 | + |
| 204 | + default: |
| 205 | + cout << "Error: Invalid selection. Please try again!" << endl; |
| 206 | + } |
| 207 | + }while(select !=10); |
| 208 | + |
| 209 | + outFile.close(); |
| 210 | + |
| 211 | + bye(); |
| 212 | + |
| 213 | + return 0; |
| 214 | +} |
| 215 | + |
| 216 | +int menuSelection(void) |
| 217 | +// Function: Displays Preconditions for the program and the Switch case menu. |
| 218 | +// Precondition: Initializes select. |
| 219 | +// Postcondition: Return users choice. |
| 220 | +{ |
| 221 | + int select; |
| 222 | + |
| 223 | + cout << "\t Binary Search Tree Operations \n" << endl; |
| 224 | + |
| 225 | + // Menu choices |
| 226 | + cout << " MENU OPTIONS " << endl; |
| 227 | + cout << "1. Print ancestors(iterative)" << endl; |
| 228 | + cout << "2. Insert" << endl; |
| 229 | + cout << "3. Delete" << endl; |
| 230 | + cout << "4. Print ancestors(reverse)" << endl; |
| 231 | + cout << "5. Make Tree" << endl; |
| 232 | + cout << "6. Print Tree" << endl; |
| 233 | + cout << "7. Leaf Count" << endl; |
| 234 | + cout << "8. Single Parent Count" << endl; |
| 235 | + cout << "9. Node count with value less than parameter value" << endl; |
| 236 | + cout << "10. Quit" << endl; |
| 237 | + cout << "Select:_"; |
| 238 | + cin >> select; |
| 239 | + |
| 240 | + return select; |
| 241 | +} |
| 242 | + |
| 243 | +// Function to call Add Elements |
| 244 | +void MakeTree(TreeType& tree, int info[],int toIndex,int fromIndex) |
| 245 | +{ |
| 246 | + AddElements(tree, info, toIndex, fromIndex); |
| 247 | +} |
| 248 | + |
| 249 | +// Function that runs Make tree function to print elements in sorted order |
| 250 | +void AddElements(TreeType &tree, int info[], int toIndex, int fromIndex) |
| 251 | +{ |
| 252 | + if(toIndex < fromIndex) |
| 253 | + { |
| 254 | + cout << "\n No more elements can be inserted" << endl; |
| 255 | + return; |
| 256 | + } |
| 257 | + else |
| 258 | + { |
| 259 | + tree.PutItem(info[fromIndex]); |
| 260 | + fromIndex++; |
| 261 | + MakeTree(tree,info,toIndex,fromIndex); |
| 262 | + } |
| 263 | + |
| 264 | +} |
| 265 | + |
| 266 | +typedef int ItemType; |
| 267 | +int Count(TreeType& tree, ItemType value) |
| 268 | +// Pre: Tree has been initialized. |
| 269 | +// Post: Function value = the number of nodes in tree that contain values that are less than value. |
| 270 | +{ |
| 271 | + ItemType item; |
| 272 | + bool finished = false; |
| 273 | + int number = 0; // Sum of number of items < value |
| 274 | + |
| 275 | + if (tree.IsEmpty()) |
| 276 | + return 0; |
| 277 | + else{ |
| 278 | + tree.ResetTree(IN_ORDER); |
| 279 | + // By using an inorder traversal, the process can stop when a larger value is returned. |
| 280 | + while(!finished) |
| 281 | + { |
| 282 | + tree.GetNextItem(item, IN_ORDER, finished); |
| 283 | + if (item < value) |
| 284 | + number ++; |
| 285 | + else |
| 286 | + finished = true; |
| 287 | + } |
| 288 | + return number; |
| 289 | + } |
| 290 | +} |
0 commit comments