Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can someone fix my code about tree in c++ to make it output like this: *******Ou

ID: 3808033 • Letter: C

Question

can someone fix my code about tree in c++ to make it output like this:

*******Output*****

Adding 100

Adding 200

Adding 400

Preorder Print: 300 100 200 400

Inorder Print: 100 200 300 400

Postorder Print: 100 200 400 300

Node 500 not found

Node 600 not found

Min=100

Max=400

Successor to 300=400

Predecessor to 300=200

Deleting 300

Preorder Print: 200 100 400

Deleting entire tree pointer

_________________________________

main.cpp:

#include <iostream>

#include "BSTree.h"

using namespace std;

int main()

{

BSTree *obj = new BSTree();

   obj->addNode(300);

obj->addNode(100);

obj->addNode(200);

obj->addNode(400);

obj->printPreorder(obj->Root());

cout<<"Preorder Print";

obj->printInorder(obj->Root());

cout<<"Inorder Print";

obj->printPostorder(obj->Root());

cout<<"Post Print";

  

obj->findNode(100, obj->Root());

obj->findNode(200, obj->Root());

obj->findNode(300, obj->Root());

obj->findNode(500, obj->Root());

obj->findNode(600, obj->Root());

cout<<"MIN="<min(obj->Root())->Key()<<endl;

cout<<"MAX="<max(obj->Root())->Key()<<endl;

cout<<"Successor to 300="<successor(300, obj->Root())->Key()<<endl;

cout<<"Predecessorto 300="<predecessor(300, obj->Root())->Key()<endl;

obj->deleteNode(300);

obj->printPreorder(obj->Root());

  

}

___________________________________

Node.h

----------------------------------------------------------------------------------------------------------------------------------------

BSTree.h

BSTree.cpp

________________

please only make changes to  printPreorder, printInorder, printPostorder, and findNode member functions and main.cpp.

thanks for the help in advance!

will thump up.

Explanation / Answer

//chamged one file and 4 functions listed in BSTree.cpp

// Print the BSTree
void BSTree::printPreorder(Node* node)
{
   if (node != nullptr)
   {
       cout << node->Key() << endl;
       if (node->Left() != NULL)
           printPreorder(node->Left());
       if (node->Right() != NULL)
           printPreorder(node->Right());
      
   }
}
void BSTree::printInorder(Node* node)
{
   if (node != nullptr)
   {
      
       if (node->Left() != NULL)
           printPreorder(node->Left());
       cout << node->Key() << endl;
       if (node->Right() != NULL)
           printPreorder(node->Right());
      
   }

}

void BSTree::printPostorder(Node* node)
{
   if (node != nullptr)
   {
       if (node->Left() != NULL)
           printPreorder(node->Left());
       if (node->Right() != NULL)
           printPreorder(node->Right());
       cout << node->Key() << endl;
   }
}

// Find a node
Node* BSTree::findNode(int key, Node* node)
{
   if (node != nullptr)
   {
       if (node->Key() == key)
           return node;
       findNode(key, node->Left());
       findNode(key, node->Right());
   }
}

---------------------------------------------------------------------------------------------------------------------------

//output

300
100
200
400
Preorder Print100
200
300
400
Inorder Print100
200
400
300
Post PrintMIN=100
MAX=400
Successor to 300=400
Predecessorto 300=200
400
100
200

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote