#include using namespace std; //===================================================================== struct Node { int x; Node * left; Node * right; Node(){left = NULL; right = NULL;} Node(int d,...

1 answer below »

Add methods to the Tree class ofBST_Basic-004cpp.cpp

download

to do the following:


1) find1, an iterative version of find that takes an int parameter and looks for it in the tree. Return NULL if not found, or a pointer to the Node containing the value if found.


2) find2, a recursive version of the method.


3) node_count, returns a count of the number of nodes in the tree.


4) depth, returns the depth of the deepest node in the tree. A null tree will have depth zero, a one node tree depth 1.






#include using namespace std; //===================================================================== struct Node { int x; Node * left; Node * right; Node(){left = NULL; right = NULL;} Node(int d, Node* l = 0, Node* r = 0) { x = d; left = l; right = r; } }; //===================================================================== class Tree { private: Node * root; public: Tree() { root = NULL; } //----------------------------------------------------------------- void insert(int x) { Node * ptr = new Node(x); cout < "\n-------------------------------------"="">< endl;="" cout="">< "node="" address:="" "="">< ptr="">< endl;="" if="" (root="=" null)="" root="ptr;" else="" {="" node="" *="" temp="root;" while="" (true)="" {="" if="" (temp="" -=""> x > x) { if (temp -> left == NULL) { cout < "node="" parent="" address:="" "="">< temp="">< endl;="" cout="">< "node="" parent="" value:="" "="">< temp="" -=""> x < endl;="" temp="" -=""> left = ptr; break; } else temp = temp -> left; } else { if (temp -> right == NULL) { cout < "node="" parent="" address:="" "="">< temp="">< endl;="" cout="">< "node="" parent="" value:="" "="">< temp="" -=""> x < endl;="" temp="" -=""> right = ptr; break; } else temp = temp -> right; } } } cout < "root:="" "="">< root="">< endl;="" cout="">< "-------------------------------------"="">< endl;="" }="" -----------------------------------------------------------------="" public:="" void="" inorder(){="" inorder(root);="" }="" private:="" void="" inorder(node="" *="" temp)="" {="" if="" (temp="" !="NULL)" {="" inorder(temp="" -=""> left); cout < temp="" -=""> x < "="" ";="" inorder(temp="" -=""> right); } } //----------------------------------------------------------------- public: void preOrder(){ inOrder(root); } private: void preOrder(Node * temp) { if (temp != NULL) { cout < temp="" -=""> x < "="" ";="" preorder(temp="" -=""> left); preOrder(temp -> right); } } //----------------------------------------------------------------- public: void postOrder(){ inOrder(root); } private: void postOrder(Node * temp) { if (temp != NULL) { postOrder(temp -> left); postOrder(temp -> right); cout < temp="" -=""> x < "="" ";="" }="" }="" -----------------------------------------------------------------="" public:="" void="" serial(){="" serial(root);="" }="" private:="" void="" serial(node="" *n)="" {="" cout="">< '(';="" if="" (n="" !="0)" {="" cout="">< n-="">x < '="" ';="" serial(n-="">left); cout < '="" ';="" serial(n-="">right); } cout < ')';="" }="" -----------------------------------------------------------------="" public:="" node="" *="" getroot()="" {="" return="" root;="" }="" -----------------------------------------------------------------="" public:="" void="" display(){="" display(root);="" }="" private:="" void="" display(node="" *="" temp)="" {="" if="" (temp="" !="NULL)" {="" cout="">< endl;="" cout="">< "parent:="" "="">< temp="" -=""> x < endl;="" cout="">< "="" left="" child="" of="" "="">< temp="" -=""> x < ":="" ";="" if="" (temp="" -=""> left == NULL) cout < "null"="">< endl;="" else="" cout="">< temp="" -=""> left -> x < endl;="" cout="">< "="" right="" child="" of="" "="">< temp="" -=""> x < ":="" ";="" if="" (temp="" -=""> right == NULL) cout < "null"="">< endl;="" else="" cout="">< temp="" -=""> right -> x < endl;="" display(temp="" -=""> left); display(temp -> right); } } //----------------------------------------------------------------- bool isEmpty() { if (root == NULL) return true; return false; } public: //----------------------------------------------------------------- void removeAll(int value) { while (true) { Node * temp = root, * ptemp = root; while (temp != NULL && temp -> x != value) { ptemp = temp; if (temp -> x < value)="" temp="temp" -=""> right; else if (temp -> x > value) temp = temp -> left; } if (temp == NULL) {
Answered Same DayMar 28, 2021

Answer To: #include using namespace std;...

Sandeep Kumar answered on Mar 29 2021
158 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here