Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

EDIT: I noted what the project 1 piece refers to, but if it still isn't clear, feel free to skip that part (the last part).

EDIT: I noted what the project 1 piece refers to, but if it still isn't clear, feel free to skip that part (the last part).

EDIT 2: Not sure what files are missing. All you should need is BinarySearchTree.h (which is posted below the instructions). You can skip the part referring to project 1!image text in transcribed

3 notes on this:

1.) The code for BinarySearchTree.h is below

2.) The last part refers to a "type" from project 1. This is referring to the type "Combine", stemming from a class I wrote, called Combine.h (which is code that takes a text file containing information on different football players and their combine results and stores those players as a vector of objects of the class). That code is also below

3.) You may need to overload the , = and == operators of the class from Project 1.

BinarySearchTree.h:

#ifndef PROJECT3STARTER_BINARYSEARCHTREE_H #define PROJECT3STARTER_BINARYSEARCHTREE_H template class BinarySearchTree { private: struct BinaryNode { Comparable value; BinaryNode* leftChild; BinaryNode* rightChild; // Constructors  BinaryNode() : value(Comparable()), leftChild(nullptr), rightChild(nullptr) {} explicit BinaryNode(Comparable c) : value(c), leftChild(nullptr), rightChild(nullptr) {} BinaryNode(Comparable c, BinaryNode* l, BinaryNode* r) : value(c), leftChild(l), rightChild(r) {} }; BinaryNode* root; // Helper recursive function to destroy the tree.  void destroy(BinaryNode* &n) { if (n != nullptr) { destroy(n->leftChild); destroy(n->rightChild); delete n; n = nullptr; } } // Helper recursive function to copy the tree.  BinaryNode* copyNode(BinaryNode* n) { return (n == nullptr)? nullptr : new BinaryNode(n->value, copyNode(n->leftChild), copyNode(n->rightChild)); } // Helper recursive function to find a value in the tree.  bool find(const Comparable &c, BinaryNode* n) const { if (n == nullptr) { // Reached a dead end. Value not in tree.  return false; } if (c value) { // Value is less than current node. Go to node's left child.  return find(c, n->leftChild); } if (n->value // Value is greater than current node. Go to node's right child.  return find(c, n->rightChild); } // If code reaches here, c == n->value. Node found!  return true; } // Helper recursive function to add a value to the tree.  void add(const Comparable &c, BinaryNode* &n) { if (n == nullptr) { // We found the place where we can add the node.  n = new BinaryNode(c, nullptr, nullptr); } else if (c value) { // Value is less than current node. Go to left child.  add(c, n->leftChild); } else if (n->value // Value is greater than current node. Go to right child.  add(c, n->rightChild); } // If code reaches here, value is a duplicate. Nothing to do.  } // Helper recursive method to find the maximum value from a given node.  Comparable& findMax(BinaryNode* n) { if (n->rightChild == nullptr) { return n->value; } return findMax(n->rightChild); } // Helper recursive function to delete a value from the tree.  void remove(const Comparable &c, BinaryNode* &n) { if (n == nullptr) { // We did not find the value. Cannot remove it. Nothing to do.  return; } else if (c value) { // Value is less than current node. Go to left child.  remove(c, n->leftChild); } else if (n->value // Value is greater than current node. Go to right child.  remove(c, n->rightChild); } // If code reaches here, we found the node. Now to remove it.  else if (n->leftChild != nullptr && n->rightChild != nullptr) { // The node we want to remove has two children  // Find the largest value from the left subtree  n->value = findMax(n->leftChild); remove(n->value, n->leftChild); } else { // The node we want to remove has 0 or 1 child.  // If it has a child, move it up. If not, set to nullptr.  BinaryNode *oldNode = n; n = (n->leftChild != nullptr) ? n->leftChild : n->rightChild; delete oldNode; oldNode = nullptr; } } public: // Default Constructor  BinarySearchTree() { root = nullptr; } // Copy Constructor  BinarySearchTree(const BinarySearchTree &b) { // calls private helper function  root = copyNode(b.root); } // Destructor  ~BinarySearchTree() { // calls private helper function  destroy(root); } // Method to destroy tree  void timber() { // calls private helper function  destroy(root); } bool isEmpty() const { return (root == nullptr); } bool find(const Comparable &c) const { // calls private helper function  return find(c, root); } void add(const Comparable &c) { // calls private helper function  add(c, root); } void remove(const Comparable &c) { // calls private helper function  remove(c, root); } // Overloaded = operator  BinarySearchTree& operator = (const BinarySearchTree &rhs) { root = copyNode(rhs.root); } }; #endif //PROJECT3STARTER_BINARYSEARCHTREE_H 

Combine.h:

#ifndef COMBINE_H_ #define COMBINE_H_ #include #include #include #include #include #include using namespace std; class Combine { private: string name, college, pos; int height, weight; float dash, bench; public: //CONSTRUCTORS  Combine() { name = "John Smith"; college = "University"; pos = "player"; height = 0; weight = 0; dash = 0; bench = 0; } Combine(string name, string college, string pos, int height, int weight, float dash, float bench) { this->name = name; this->college = college; this->pos = pos; setHeight(height); setWeight(weight); setDash(dash); setBench(bench); } //GETTERS  string getName() const { return name; } string getCollege() const { return college; } string getPos() const { return pos; } int getHeight() const { return height; } int getWeight() const { return weight; } float getDash() const { return dash; } float getBench() const { return bench; } //SETTERS  void setName(string name) { this->name = name; } void setCollege(string college) { this->college = college; } void setPos(string pos) { this->pos = pos; } void setHeight(int height) { if (height //Default value  this->height = 0; } else { this->height = height; } } void setWeight(int weight) { if (weight //Default value  this->weight = 0; } else { this->weight = weight; } } void setDash(float dash) { if (dash //Default value  this->dash = 0; } else { this->dash = dash; } } void setBench(float bench) { if (bench //Default value  this->bench = 0; } else { this->bench = bench; } } // Overloaded operators  friend ostream& operator/* getPlayersFromFile reads the file containing the dataset in and assigns each entry its attributes.  * The function is similar to what we looked at in class; however, I made one major change which was including stringstream  * simply for better handling of values  */ void getPlayersFromFile(string filename, vector &players) { ifstream inFile; // Use ../ to get out of the cmake-build-debug folder to find the file  inFile.open("../" + filename); // inFile.open(filename);  string name = "", college = "", pos = "", header = ""; string temp; int height = 0, weight = 0; float dash = 0, bench = 0; char comma = ','; // check that the file is in a good state  if (inFile) { // read in the header line  getline(inFile, header); // loop through all the data in the file  while (inFile && inFile.peek() != EOF) { // read line  getline(inFile,temp); // pass it to string stream  stringstream ss(temp); // get name  getline(ss,name,','); // get college  getline(ss,college,','); // get POS  getline(ss,pos,','); // get height  getline(ss,temp,','); height = atoi(temp.c_str()); // get weight  getline(ss,temp,','); weight = atoi(temp.c_str()); if(ss.good()) { // get 40 yard  getline(ss,temp,','); dash = atof(temp.c_str()); } else{ dash=0; } if(ss.good()) { // get bench press  getline(ss,temp,','); bench = atof(temp.c_str()); } else{ bench=0; } // create a Combine object and put it on the back of the vector  players.push_back(Combine(name, college, pos, height, weight, dash, bench)); } } else { cerr /*  * avgWeight simply accepts the vector of the Combine class from main and iterates through each object, adding the each entry's (player's) weight to the get the sum before  * dividing by the size of the vector to calculate the average.  */ float avgWeight(vector &players) { float total = 0; getPlayersFromFile("CombinePlayers.txt", players); for (int i = 0; i //cout  } cout   Binary Search Tree - Copy the Binary Search Tree.h file from Blackboard into your CLion project. - Modify the Binary Search Tree's find methods so that a search of the tree also stores the depth of the last node visited. To do this without losing information, pass an integer by reference into both methods and modify it inside the methods. - Note: the root node is at depth 0. - Note: even if the search fails, you should still record the depths of the last node visited. Create an integer Binary Search Tree. Insert the numbers from 1 to 100 in that order. Then search for all 100 numbers in the tree and record their depths in a file. - Search for 0, 101, and 102. What return values do you get from the find method? What depths do you get? Why? - Create another integer Binary Search Tree. Insert the numbers from 1 to 100 in a random order. Then search for all 100 numbers in the tree and record their depths in a file. - Note: you can get a random sequence either from random.org or by shuffling a vector of numbers (C++ has a shuffle function). - How do these depths compare to the first file of depths? Why? - Create a Binary Search Tree of your custom data type from Project 1. Insert all 1000+ objects from the vector into the tree, then search for all the objects and write their depths to a file. - How do these depths compare to the integer BST depths? Why? - Graph these depths to compare with the other trees (do not use C++ to graph, you can use whatever spreadsheet application or graphical programming language you prefer).  Binary Search Tree - Copy the Binary Search Tree.h file from Blackboard into your CLion project. - Modify the Binary Search Tree's find methods so that a search of the tree also stores the depth of the last node visited. To do this without losing information, pass an integer by reference into both methods and modify it inside the methods. - Note: the root node is at depth 0. - Note: even if the search fails, you should still record the depths of the last node visited. Create an integer Binary Search Tree. Insert the numbers from 1 to 100 in that order. Then search for all 100 numbers in the tree and record their depths in a file. - Search for 0, 101, and 102. What return values do you get from the find method? What depths do you get? Why? - Create another integer Binary Search Tree. Insert the numbers from 1 to 100 in a random order. Then search for all 100 numbers in the tree and record their depths in a file. - Note: you can get a random sequence either from random.org or by shuffling a vector of numbers (C++ has a shuffle function). - How do these depths compare to the first file of depths? Why? - Create a Binary Search Tree of your custom data type from Project 1. Insert all 1000+ objects from the vector into the tree, then search for all the objects and write their depths to a file. - How do these depths compare to the integer BST depths? Why? - Graph these depths to compare with the other trees (do not use C++ to graph, you can use whatever spreadsheet application or graphical programming language you prefer)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions