Please write the C++ code that would display the exact same output as provided. Thank you.
main.cpp
#include "TreeNode.h"
//GIVEN
void inorderTraversal(TreeNode * root);
//TODO
TreeNode * search(TreeNode * node,int data);
//GIVEN
void insert(TreeNode * node,int data);
//GIVEN
bool isNull(TreeNode * node){
return node==nullptr;
}
int main() {
// create the root, gets set to null
TreeNode * root;
// create the roor and set the data of root to 5
root= new TreeNode(5);
//create two additional nodes with data values 10 and 3
// these will go to the right and left of root respectively
TreeNode * ten= new TreeNode(10);
TreeNode * three= new TreeNode(3);
//connect ten to the right of root (which has data 5)
root->setRight(ten);
//connect three to the left of root (which has data 5)
root->setLeft(three);
// note this can also be done as
//root.setRight(new TreeNode(10));
//root.setLeft(new TreeNode(3);)
// add two more nodes
//the first has data value 7. So to keep the tree in insorted order, it should get attached as the
left node of ten
// the second has data value 4. So to keep the tree in insorted order, it should get attached as the
right node of three
ten->addLeft(7);
three->addRight(4);
std::cout<<"**************************************\n";
std::cout<<"Printing the Inorder Traversal\n";
inorderTraversal(root);
std::cout<<"**************************************\n";
std::cout<<"Searching for Node \n";
TreeNode* result = search(root,4);
if (result!=nullptr){
std::cout<<"Found "<getData()<<"\n";
}
else
std::cout<<"Not Found "<<4<<"\n";
result = search(root,1);
if (result!=nullptr){
std::cout<<"Found "<getData()<<"\n";
}
else
std::cout<<"Not Found "<<1<<"\n";
std::cout<<"**************************************\n";
std::cout<<"Inserting 6\n";
insert(root,6);
std::cout<<"**************************************\n";
std::cout<<"Printing the Inorder Traversal\n";
inorderTraversal(root);
}
// uses recursion
void inorderTraversal(TreeNode * node){
// exit case
if (isNull(node)) return;
if (node->isLeaf()){
std::cout<<"Printing Leaf Node "<getData()<<"\n";
return;}
// reached a node with no left, so print the node and travel right
if (!isNull(node->getLeft()))
// if there is a left path, then travel further on that
inorderTraversal(node->getLeft());
std::cout<<"Printing Node "<getData()<<"\n";
// save and travel the right path of the current node being processed
inorderTraversal(node->getRight());
}
// uses recursion
//TODO
TreeNode * search(TreeNode * node, int data){
// if the node is null return the null ptr
//if this nodes data equals the search date return found
// if not, if the left tree exists and search data less than
//this nodes data return the result of recursive all to search with left pointer
// if no left tree, but right tree exists and search data greater than
//this nodes data return the result of recursive all to search with right pointer
// if both above conditions not true return null ptr
}
//uses recursion
void insert(TreeNode * node,int .
Please write the C++ code that would display the exact same output a.pdf
1. Please write the C++ code that would display the exact same output as provided. Thank you.
main.cpp
#include "TreeNode.h"
//GIVEN
void inorderTraversal(TreeNode * root);
//TODO
TreeNode * search(TreeNode * node,int data);
//GIVEN
void insert(TreeNode * node,int data);
//GIVEN
bool isNull(TreeNode * node){
return node==nullptr;
}
int main() {
// create the root, gets set to null
TreeNode * root;
// create the roor and set the data of root to 5
root= new TreeNode(5);
//create two additional nodes with data values 10 and 3
// these will go to the right and left of root respectively
TreeNode * ten= new TreeNode(10);
TreeNode * three= new TreeNode(3);
//connect ten to the right of root (which has data 5)
root->setRight(ten);
//connect three to the left of root (which has data 5)
root->setLeft(three);
// note this can also be done as
//root.setRight(new TreeNode(10));
//root.setLeft(new TreeNode(3);)
// add two more nodes
//the first has data value 7. So to keep the tree in insorted order, it should get attached as the
left node of ten
2. // the second has data value 4. So to keep the tree in insorted order, it should get attached as the
right node of three
ten->addLeft(7);
three->addRight(4);
std::cout<<"**************************************n";
std::cout<<"Printing the Inorder Traversaln";
inorderTraversal(root);
std::cout<<"**************************************n";
std::cout<<"Searching for Node n";
TreeNode* result = search(root,4);
if (result!=nullptr){
std::cout<<"Found "<getData()<<"n";
}
else
std::cout<<"Not Found "<<4<<"n";
result = search(root,1);
if (result!=nullptr){
std::cout<<"Found "<getData()<<"n";
}
else
std::cout<<"Not Found "<<1<<"n";
std::cout<<"**************************************n";
std::cout<<"Inserting 6n";
insert(root,6);
std::cout<<"**************************************n";
std::cout<<"Printing the Inorder Traversaln";
inorderTraversal(root);
}
// uses recursion
void inorderTraversal(TreeNode * node){
3. // exit case
if (isNull(node)) return;
if (node->isLeaf()){
std::cout<<"Printing Leaf Node "<getData()<<"n";
return;}
// reached a node with no left, so print the node and travel right
if (!isNull(node->getLeft()))
// if there is a left path, then travel further on that
inorderTraversal(node->getLeft());
std::cout<<"Printing Node "<getData()<<"n";
// save and travel the right path of the current node being processed
inorderTraversal(node->getRight());
}
// uses recursion
//TODO
TreeNode * search(TreeNode * node, int data){
// if the node is null return the null ptr
//if this nodes data equals the search date return found
// if not, if the left tree exists and search data less than
//this nodes data return the result of recursive all to search with left pointer
// if no left tree, but right tree exists and search data greater than
//this nodes data return the result of recursive all to search with right pointer
// if both above conditions not true return null ptr
}
//uses recursion
void insert(TreeNode * node,int data){
if (node->getData()==data)
return;
else if(datagetData()){
if (!isNull(node->getLeft()))
// std::cout<<"Going Left from "<getData()<<"n";
return insert(node->getLeft(),data);
else node->setLeft(new TreeNode(data));
5. //
}
TreeNode * TreeNode::getRight(){
return right;
}
// does not create any new node. Just sets the right pointer
void TreeNode::setRight(TreeNode *newRight){
right=newRight;
}
// does not create any new node. Just sets the left pointer
//TODO
void TreeNode::setLeft(TreeNode *newLeft){
//
}
bool TreeNode::isLeaf(){
return (left==nullptr&&right==nullptr);
}
//creates a new node with the data value and sets the rightpointer to it
void TreeNode::addRight(int data){
right= new TreeNode(data,nullptr,nullptr);
}
//creates a new node with the data value and sets the left pointer to it
//TODO
void TreeNode::addLeft(int data){
//
}
TreeNode.h
#include
#include
#include
class TreeNode{
private: int data;
private: TreeNode * left;
private: TreeNode * right;
6. public:
TreeNode();
TreeNode(int data);
TreeNode(int data, TreeNode * left, TreeNode * right);
void setData(int data);
int getData();
bool isLeaf();
TreeNode * getLeft();
TreeNode * getRight();
void setRight(TreeNode *newRight);
void setLeft(TreeNode *newLeft);
void addRight(int data);
void addLeft(int data);
};
Make sure that the completed code would display the below output:
**************************************
Printing the Inorder Traversal
Printing Node 3
Printing Leaf Node 4
Printing Node 5
Printing Leaf Node 7
Printing Node 10
**************************************
Searching for Node
Found 4
Not Found 1
**************************************
Inserting 6
**************************************
Printing the Inorder Traversal
Printing Node 3
Printing Leaf Node 4
Printing Node 5
7. Printing Leaf Node 6
Printing Node 7
Printing Node 10
Description You are given a template that contains the TreeNode class header and partially
implemented TreeNode.cpp file. A driver is also included. This driver constructs TreeNode
objects and connects their pointers, manually creating a sorted binary tree. Methods to insert
nodes while keeping the tree sorted is implemented as also to traverse the tree "inorder". A
search method that traverses the tree to search for a given data value needs to be written.
Recursion is used in all these methods. The output file is also given to you TreeNode private: int
data; private: TreeNode * left; private: TreeNode * right; public: TreeNode(); //default
constructor: sets data to 0, pointers to nullptr TreeNode(int data); //custom constructor: sets this
data to data, pointers to nullptr TreeNode(int data, TreeNode * left, TreeNode * right); custom
constructor, sets data and pointers void setData(int data); // sets data int getData(); // returns data
bool isLeaf(); // returns true if both pointers are null. TreeNode * getLeft(); // returns left Pointer
TreeNode * getRight(); //returns left Pointer void setRight(TreeNode *newRight); sets right
Pointer void setLeft(TreeNode *newLeft); sets left Pointer void addRight(int data); adds a new
TreeNode with this data to the right of current TreeNode void addLeft(int data); adds a new
TreeNode with this data to the left of current TreeNode };
Most of these methods have been implemented. Please implement the "right" methods by
examining the "left" methods TreeNode * getLeft(); // returns left Pointer void setLeft(TreeNode
*newLeft); sets left Pointer void addLeft(int data); adds a new TreeNode with this data to the left
of current TreeNode
# The driver is given is given to you that does the following 1. Creates a root TreeNode with
data value 5 2. Create two additional nodes with data values 10 and 3 3. Set these to the right and
left of root respectively 4. Add two more nodes- the first has data value 7. So to keep the tree in
insorted order, it should get attached as the left node of ten 5. Add the second node with data
value 4 . So to keep the tree in insorted order, it should get attached as the right node of three 6.
Call the inorderTraversal methods that traverses the tree starting from the root, in sorted order
and prints the nodes visited.
7. Search for an existing node with data value 4 . 8. Search for a non existing node with data
value 1. 9. Insert a new node with data value 6 . 10. Call the inorderTraversal methods that
traverses the tree starting from the root, in sorted order and prints the nodes visited.