SlideShare a Scribd company logo
1 of 7
Download to read offline
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 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){
// 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));
}
else if (data>node->getData()){
if (!isNull(node->getRight()))
return insert(node->getRight(),data);
else node->setRight(new TreeNode(data));
}
}
TreeNode.cpp
#include "TreeNode.h"
TreeNode::TreeNode(){
data=0;
left=nullptr;
right=nullptr;
}
TreeNode::TreeNode(int data){
this->data=data;
this->left=nullptr;
this->right=nullptr;
}
TreeNode::TreeNode(int data,TreeNode * left, TreeNode * right){
this->data=data;
this->left=left;
this->right=right;
}
void TreeNode::setData(int data){
this->data=data;
}
int TreeNode::getData(){
return data;
}
//TODO
TreeNode * TreeNode::getLeft(){
//
}
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;
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
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.

More Related Content

Similar to Please write the C++ code that would display the exact same output a.pdf

A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfanton291
 
Program to insert in a sorted list #includestdio.h#include.pdf
 Program to insert in a sorted list #includestdio.h#include.pdf Program to insert in a sorted list #includestdio.h#include.pdf
Program to insert in a sorted list #includestdio.h#include.pdfsudhirchourasia86
 
C++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdfC++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdfarjunenterprises1978
 
Tree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfTree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfajayadinathcomputers
 
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdfWrite the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdffathimalinks
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxBrianGHiNewmanv
 
Use C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfUse C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfshalins6
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfJUSTSTYLISH3B2MOHALI
 
MAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdfMAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdfadityastores21
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfrohit219406
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxfarrahkur54
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
 
Write java program using linked list to get integer from user and.docx
 Write java program using linked list to get integer from user and.docx Write java program using linked list to get integer from user and.docx
Write java program using linked list to get integer from user and.docxajoy21
 
Write a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docxWrite a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docxnoreendchesterton753
 
C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked listSourav Gayen
 
pleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdfpleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdfwasemanivytreenrco51
 

Similar to Please write the C++ code that would display the exact same output a.pdf (20)

A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
 
Program to insert in a sorted list #includestdio.h#include.pdf
 Program to insert in a sorted list #includestdio.h#include.pdf Program to insert in a sorted list #includestdio.h#include.pdf
Program to insert in a sorted list #includestdio.h#include.pdf
 
C++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdfC++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdf
 
Tree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfTree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdf
 
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdfWrite the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docx
 
Use C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfUse C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdf
 
Linked list
Linked listLinked list
Linked list
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
Lab12 dsa bsee20075
Lab12 dsa bsee20075Lab12 dsa bsee20075
Lab12 dsa bsee20075
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
MAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdfMAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdf
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
Write java program using linked list to get integer from user and.docx
 Write java program using linked list to get integer from user and.docx Write java program using linked list to get integer from user and.docx
Write java program using linked list to get integer from user and.docx
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
Write a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docxWrite a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docx
 
C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked list
 
pleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdfpleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdf
 

More from amarndsons

Plot the marginal utility of food. Remember to plot using midpoints#.pdf
Plot the marginal utility of food. Remember to plot using midpoints#.pdfPlot the marginal utility of food. Remember to plot using midpoints#.pdf
Plot the marginal utility of food. Remember to plot using midpoints#.pdfamarndsons
 
Please write the mysh program in C and follow the guidelines specifi.pdf
Please write the mysh program in C and follow the guidelines specifi.pdfPlease write the mysh program in C and follow the guidelines specifi.pdf
Please write the mysh program in C and follow the guidelines specifi.pdfamarndsons
 
Please write the program mysh in C programming language and follow.pdf
Please write the program mysh in C programming language and follow.pdfPlease write the program mysh in C programming language and follow.pdf
Please write the program mysh in C programming language and follow.pdfamarndsons
 
please write in C programming Write in C code that is able to rea.pdf
please write in C programming  Write in C code that is able to rea.pdfplease write in C programming  Write in C code that is able to rea.pdf
please write in C programming Write in C code that is able to rea.pdfamarndsons
 
please will you answer the following questions about chromatin struc.pdf
please will you answer the following questions about chromatin struc.pdfplease will you answer the following questions about chromatin struc.pdf
please will you answer the following questions about chromatin struc.pdfamarndsons
 
Please write a detailed post responding to this question.Question.pdf
Please write a detailed post responding to this question.Question.pdfPlease write a detailed post responding to this question.Question.pdf
Please write a detailed post responding to this question.Question.pdfamarndsons
 
Please write a detailed assessment of the long-term advantages and d.pdf
Please write a detailed assessment of the long-term advantages and d.pdfPlease write a detailed assessment of the long-term advantages and d.pdf
Please write a detailed assessment of the long-term advantages and d.pdfamarndsons
 
PLEASE USE TuProlog for this. Also the facts need to be Binary Fac.pdf
PLEASE USE TuProlog for this. Also the facts need to be Binary Fac.pdfPLEASE USE TuProlog for this. Also the facts need to be Binary Fac.pdf
PLEASE USE TuProlog for this. Also the facts need to be Binary Fac.pdfamarndsons
 
Please use your own word and dont copy and paste the anwser from ke.pdf
Please use your own word and dont copy and paste the anwser from ke.pdfPlease use your own word and dont copy and paste the anwser from ke.pdf
Please use your own word and dont copy and paste the anwser from ke.pdfamarndsons
 
Please use one of the 2 Excel files (BirdSrtike or Super-SampleStore.pdf
Please use one of the 2 Excel files (BirdSrtike or Super-SampleStore.pdfPlease use one of the 2 Excel files (BirdSrtike or Super-SampleStore.pdf
Please use one of the 2 Excel files (BirdSrtike or Super-SampleStore.pdfamarndsons
 
Please state whether each of the following statements is True or Fal.pdf
Please state whether each of the following statements is True or Fal.pdfPlease state whether each of the following statements is True or Fal.pdf
Please state whether each of the following statements is True or Fal.pdfamarndsons
 
Please tell me why I get this error in my code I have the screen sh.pdf
Please tell me why I get this error in my code I have the screen sh.pdfPlease tell me why I get this error in my code I have the screen sh.pdf
Please tell me why I get this error in my code I have the screen sh.pdfamarndsons
 
Please teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdfPlease teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdfamarndsons
 
please solve this problem using R language 3. The following data .pdf
please solve this problem using R language  3. The following data .pdfplease solve this problem using R language  3. The following data .pdf
please solve this problem using R language 3. The following data .pdfamarndsons
 
Please show all the necessary steps to find the above op code Q2. (1.pdf
Please show all the necessary steps to find the above op code Q2. (1.pdfPlease show all the necessary steps to find the above op code Q2. (1.pdf
Please show all the necessary steps to find the above op code Q2. (1.pdfamarndsons
 
Please show all steps i want to understand whats happening. 2- Solve.pdf
Please show all steps i want to understand whats happening. 2- Solve.pdfPlease show all steps i want to understand whats happening. 2- Solve.pdf
Please show all steps i want to understand whats happening. 2- Solve.pdfamarndsons
 
Please Show all work. 1. Show that the language L1={ann is prime } .pdf
Please Show all work. 1. Show that the language L1={ann is prime } .pdfPlease Show all work. 1. Show that the language L1={ann is prime } .pdf
Please Show all work. 1. Show that the language L1={ann is prime } .pdfamarndsons
 
please show your work (Remember You may find the standard normal ta.pdf
please show your work (Remember You may find the standard normal ta.pdfplease show your work (Remember You may find the standard normal ta.pdf
please show your work (Remember You may find the standard normal ta.pdfamarndsons
 
Please show the math for the stepsPart 1A.Part 1BPart 1C Us.pdf
Please show the math for the stepsPart 1A.Part 1BPart 1C Us.pdfPlease show the math for the stepsPart 1A.Part 1BPart 1C Us.pdf
Please show the math for the stepsPart 1A.Part 1BPart 1C Us.pdfamarndsons
 
Please show all work and explain the answer! which of the following .pdf
Please show all work and explain the answer! which of the following .pdfPlease show all work and explain the answer! which of the following .pdf
Please show all work and explain the answer! which of the following .pdfamarndsons
 

More from amarndsons (20)

Plot the marginal utility of food. Remember to plot using midpoints#.pdf
Plot the marginal utility of food. Remember to plot using midpoints#.pdfPlot the marginal utility of food. Remember to plot using midpoints#.pdf
Plot the marginal utility of food. Remember to plot using midpoints#.pdf
 
Please write the mysh program in C and follow the guidelines specifi.pdf
Please write the mysh program in C and follow the guidelines specifi.pdfPlease write the mysh program in C and follow the guidelines specifi.pdf
Please write the mysh program in C and follow the guidelines specifi.pdf
 
Please write the program mysh in C programming language and follow.pdf
Please write the program mysh in C programming language and follow.pdfPlease write the program mysh in C programming language and follow.pdf
Please write the program mysh in C programming language and follow.pdf
 
please write in C programming Write in C code that is able to rea.pdf
please write in C programming  Write in C code that is able to rea.pdfplease write in C programming  Write in C code that is able to rea.pdf
please write in C programming Write in C code that is able to rea.pdf
 
please will you answer the following questions about chromatin struc.pdf
please will you answer the following questions about chromatin struc.pdfplease will you answer the following questions about chromatin struc.pdf
please will you answer the following questions about chromatin struc.pdf
 
Please write a detailed post responding to this question.Question.pdf
Please write a detailed post responding to this question.Question.pdfPlease write a detailed post responding to this question.Question.pdf
Please write a detailed post responding to this question.Question.pdf
 
Please write a detailed assessment of the long-term advantages and d.pdf
Please write a detailed assessment of the long-term advantages and d.pdfPlease write a detailed assessment of the long-term advantages and d.pdf
Please write a detailed assessment of the long-term advantages and d.pdf
 
PLEASE USE TuProlog for this. Also the facts need to be Binary Fac.pdf
PLEASE USE TuProlog for this. Also the facts need to be Binary Fac.pdfPLEASE USE TuProlog for this. Also the facts need to be Binary Fac.pdf
PLEASE USE TuProlog for this. Also the facts need to be Binary Fac.pdf
 
Please use your own word and dont copy and paste the anwser from ke.pdf
Please use your own word and dont copy and paste the anwser from ke.pdfPlease use your own word and dont copy and paste the anwser from ke.pdf
Please use your own word and dont copy and paste the anwser from ke.pdf
 
Please use one of the 2 Excel files (BirdSrtike or Super-SampleStore.pdf
Please use one of the 2 Excel files (BirdSrtike or Super-SampleStore.pdfPlease use one of the 2 Excel files (BirdSrtike or Super-SampleStore.pdf
Please use one of the 2 Excel files (BirdSrtike or Super-SampleStore.pdf
 
Please state whether each of the following statements is True or Fal.pdf
Please state whether each of the following statements is True or Fal.pdfPlease state whether each of the following statements is True or Fal.pdf
Please state whether each of the following statements is True or Fal.pdf
 
Please tell me why I get this error in my code I have the screen sh.pdf
Please tell me why I get this error in my code I have the screen sh.pdfPlease tell me why I get this error in my code I have the screen sh.pdf
Please tell me why I get this error in my code I have the screen sh.pdf
 
Please teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdfPlease teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdf
 
please solve this problem using R language 3. The following data .pdf
please solve this problem using R language  3. The following data .pdfplease solve this problem using R language  3. The following data .pdf
please solve this problem using R language 3. The following data .pdf
 
Please show all the necessary steps to find the above op code Q2. (1.pdf
Please show all the necessary steps to find the above op code Q2. (1.pdfPlease show all the necessary steps to find the above op code Q2. (1.pdf
Please show all the necessary steps to find the above op code Q2. (1.pdf
 
Please show all steps i want to understand whats happening. 2- Solve.pdf
Please show all steps i want to understand whats happening. 2- Solve.pdfPlease show all steps i want to understand whats happening. 2- Solve.pdf
Please show all steps i want to understand whats happening. 2- Solve.pdf
 
Please Show all work. 1. Show that the language L1={ann is prime } .pdf
Please Show all work. 1. Show that the language L1={ann is prime } .pdfPlease Show all work. 1. Show that the language L1={ann is prime } .pdf
Please Show all work. 1. Show that the language L1={ann is prime } .pdf
 
please show your work (Remember You may find the standard normal ta.pdf
please show your work (Remember You may find the standard normal ta.pdfplease show your work (Remember You may find the standard normal ta.pdf
please show your work (Remember You may find the standard normal ta.pdf
 
Please show the math for the stepsPart 1A.Part 1BPart 1C Us.pdf
Please show the math for the stepsPart 1A.Part 1BPart 1C Us.pdfPlease show the math for the stepsPart 1A.Part 1BPart 1C Us.pdf
Please show the math for the stepsPart 1A.Part 1BPart 1C Us.pdf
 
Please show all work and explain the answer! which of the following .pdf
Please show all work and explain the answer! which of the following .pdfPlease show all work and explain the answer! which of the following .pdf
Please show all work and explain the answer! which of the following .pdf
 

Recently uploaded

Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 

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));
  • 4. } else if (data>node->getData()){ if (!isNull(node->getRight())) return insert(node->getRight(),data); else node->setRight(new TreeNode(data)); } } TreeNode.cpp #include "TreeNode.h" TreeNode::TreeNode(){ data=0; left=nullptr; right=nullptr; } TreeNode::TreeNode(int data){ this->data=data; this->left=nullptr; this->right=nullptr; } TreeNode::TreeNode(int data,TreeNode * left, TreeNode * right){ this->data=data; this->left=left; this->right=right; } void TreeNode::setData(int data){ this->data=data; } int TreeNode::getData(){ return data; } //TODO TreeNode * TreeNode::getLeft(){
  • 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.