Question: Here is the Starter Code they gave us #ifndef BST_HPP #define BST_HPP using namespace std; struct Node{ int key; Node* left = NULL; Node* right

Here is the Starter Code they gave us
#ifndef BST_HPP #define BST_HPP
using namespace std;
struct Node{ int key; Node* left = NULL; Node* right = NULL; };
class BST{ private: Node* createNode(int data); Node* root;
/** since root is a private member we need helper functions to access root for insertion, searching and printing **/
Node* addNodeHelper(Node*, int); int getSizeHelper(Node*); int getMinValueHelper(Node*); int getMaxDepthHelper(Node*);
void printTreeHelper(Node*);
/**Destructor will call this function to recursively delete all node**/
void destroyNode(Node *root);
public:
void addNode(int); // function to insert a node in the tree. int getSize(); // function to get number of nodes in the tree int getMinValue(); // function to get minimum value in the tree int getMaxDepth(); // function to get depth of the tree // bool searchKey(int); // function to search a data in the tree void printTree(); //function to print the tree
BST(); // default constructor BST(int data); // parameterized constructor ~BST(); // destructor // void deleteNode(int);
}; #endif
---------
#include
/** Create a node with key as data **/
Node* BST:: createNode(int data) { Node* newNode = new Node; newNode->key = data; newNode->left = NULL; newNode->right = NULL; return newNode; }
BST::BST() {
}
/** parameterized constructor. It will create the root and put the data in the root. **/
BST::BST(int data) { root = createNode(data); }
/** Destructor **/
BST::~BST(){ destroyNode(root); }
/** This function will destroy the subtree rooted at currNode. Think about in what order should you destroy. POSTORDER. or right-left-root **/
void BST:: destroyNode(Node *currNode) { if(currNode!=NULL) { destroyNode(currNode->left); destroyNode(currNode->right); delete currNode; currNode = NULL; } }
//--------------------------------------------INSERT NODE IN THE TREE--------------------------------------
/** This function will add the data in the tree rooted at currNode. We will call this function from addNode. NOTE: root is a private member of BST. Hence addNode function can not diretly access it. So we need this helper function. Implement your code for inserting a node in this function. **/
Node* BST:: addNodeHelper(Node* currNode, int data) {
return currNode; }
/** This function will insert the data in the tree. As this function can not access the rooted it will call the addNodeHelper function. **/
void BST:: addNode( int data) {
}
//--------------------------------------------GET NUMBER OF NODES-------------------------------------------
int BST::getSizeHelper(Node* currNode) { return 0; }
int BST::getSize() { return getSizeHelper(root); } //-------------------------------------------GET MINIMUM VALUE FROM THE TREE---------------------------------
/** Find the minimum in the subtree rooted ar currNode Go to the left most node of this subtree. **/
int BST::getMinValueHelper(Node* root) { return 0; }
int BST::getMinValue() {
}
//--------------------------------------------GET MAXIMUM DEPTH OF THE TREE----------------------------------
int BST::getMaxDepthHelper(Node* currNode) {
return 0; }
int BST::getMaxDepth() {
}
void BST:: printTreeHelper(Node* curNode) { if(curNode) { printTreeHelper( curNode->left); cout key; printTreeHelper( curNode->right); } } void BST:: printTree(){ printTreeHelper(root); cout
--------------------------
#include
int main (int argc, char* argv[]){ BST tree(5);
tree.addNode(3); //left child to 5 tree.addNode(9); //right child to 5
tree.addNode(1); //left child to 3 tree.addNode(4); //right child to 3
tree.addNode(6); //left child to 9
tree.addNode(2); //left child to 4 // insert(tmp,4);
tree.printTree(); cout
----------------
In C++ please and make sure it runs with the given main func, thanks
Using the following struct, create a program with the listed functions below. struct Node int key Node 1efti Node right; Functions 1. Node addNode int value) Insert the new value into the BST in the correct position. 2. int getSize ) 3. int getMinValue ) 4. int getMaxDepth ) Return the total number of nodes in the BST Return the minimal value in the BST. Return the maximum depth of a path in the tree
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
