[Solved]Lab Modify Anything Treenodeh Testcpp Work Implement Methods Treeh File Treeh File Please Q37232863
In this lab, do NOT modify anything in treeNode.h and test.cpp.Your work is to implement the methods in tree.h file. In the tree.hfile, please implement the following methods:
• treeBSearch(V x, TreeNode* t)
• isLeaf(TreeNode* t)
• height(TreeNode* t)
• nNodes()
• insert(V x)
The execution result of test.cpp is:
arden$ ./a.out
1
0
8
//////////////////////////////////
treeNode.h
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
template <class T>
class TreeNode{
T datum;
TreeNode<T>* left, * right;
public:
// constructor with datum value, left and right arenullptr
TreeNode(T x){
datum=x;
left = nullptr;
right = nullptr;
}
// constructor with datum value, left and rightvalues
TreeNode(T x, TreeNode<T>* lft, TreeNode<T>*rgt){
datum = x;
left = lft;
right = rgt;
}
//destructor releases left and right nodes, if notnullptr
~TreeNode(){
if (left) {
delete left;
}
if (right) {
delete right;
}
}
// get datum value
T getDatum(){
return datum;
}
// get left pointer
TreeNode<T>* getLeft(){
return left;
}
// get right pointer
TreeNode<T>* getRight(){
return right;
}
// set the left pointer
void setLeft(TreeNode<T>* p){
left = p;
}
// set the right pointer
void setRight(TreeNode<T>* p){
right = p;
}
};
/////////////////////////////
tree.h
#include “treeNode.h”
#include <iomanip>
template <class V>
class tree {
TreeNode<V> * root;
int size;
public:
// default constructor
// by default, the tree is empty
tree(){
root = nullptr;
size = 0;
}
// search value x in tree rooted at node t
bool treeSearch(V x, TreeNode<V>* t){
if(t == nullptr)return false;
if(t->getDatum() == x)return true;
return treeSearch(x, t->getLeft()) ||treeSearch(x, t->getRight());
}
bool treeSearch(V x){
treeSearch(x, root);
}
// binary search value x in tree rooted at node t
bool treeBSearch(V x, TreeNode<V>*t){
// implement this method
return false;
}
bool treeBSearch(V x){
return treeBSearch(x, root);
}
// check node t is leaf
bool isLeaf(TreeNode<V>* t){
//implement this method
return false;
}
// find the height of the tree rooted at node t
int height(TreeNode<V>* t){
//implement this method
return 0;
}
int height(){
return height(root);
}
// find the number of nodes of tree rooted at t
int nNodes(TreeNode<V>* t){
//implement this method
return 0;
}
int nNodes(){
return nNodes(root);
}
// insert value x to the current tree object
void insert(V x){
// implement this method
}
// print out the values of tree rooted at x
// it shows the hierarchy of the tree
// it will be useful for debugging
void print(TreeNode<V> * x,int indent){
if(x == nullptr)return;
if (x->getRight() !=nullptr) {
print(x->getRight(), indent+4);
}
if (indent != 0) {
cout << std::setw(indent) << ‘ ‘;
}
if(x->getRight() !=nullptr){
cout << ” /n” << std::setw(indent) << ”;
}
cout << x->getDatum() << endl;
if (x->getLeft() !=nullptr) {
cout << std::setw(indent) << ‘ ‘ <<“n”;
print(x->getLeft(), indent+4);
}
}
void print(){
int count = 0;
print(root, count);
}
};
///////////////////////
test.cpp
#include “tree.h”
int main(){
tree<int> myTree;
myTree.insert(5);
myTree.insert(3);
myTree.insert(2);
myTree.insert(4);
myTree.insert(8);
myTree.insert(6);
myTree.insert(7);
myTree.insert(9);
cout << myTree.treeBSearch(9) << endl;
cout << myTree.treeBSearch(11) << endl;
cout << myTree.nNodes() << endl;
cout << “The Tree Looks Like: ” << endl;
myTree.print();
tree<int> myTree2;
myTree2.insert(4);
myTree2.insert(2);
myTree2.insert(1);
myTree2.insert(3);
myTree2.insert(7);
myTree2.insert(5);
// myTree2.print();
}
Expert Answer
Answer to In this lab, do NOT modify anything in treeNode.h and test.cpp. Your work is to implement the methods in tree.h file. In… . . .
OR

