Hello, I looked up all the solutions that Chegg experts provided for this book (
ID: 3531417 • Letter: H
Question
Hello,
I looked up all the solutions that Chegg experts provided for this book (chapter 6) but couldnt find the explination or the answer to this question. Can someone help me please as I need to get this done ASAP and turn in.
Write a program to implement a C++ preorder iterator to supply tree nodes to the loop in Example 6.66. You will need to know (or learn) how to use pointers, references, inner classes, and operator overloading in C++. For the sake of (relative) simplicity, you may assume that the data in a tree node is always an int; this will save you the need to use generics. You may want to use the stack abstraction from the C++ standard library.
Explanation / Answer
#include using namespace std;// Node classclass Node { int key; Node* left; Node* right;public: Node() { key=-1; left=NULL; right=NULL; }; void setKey(int aKey) { key = aKey; }; void setLeft(Node* aLeft) { left = aLeft; }; void setRight(Node* aRight) { right = aRight; }; int Key() { return key; }; Node* Left() { return left; }; Node* Right() { return right; };};// Tree classclass Tree { Node* root;public: Tree(); ~Tree(); Node* Root() { return root; }; void addNode(int key); void inOrder(Node* n); void preOrder(Node* n); void postOrder(Node* n);private: void addNode(int key, Node* leaf); void freeNode(Node* leaf);};// ConstructorTree::Tree() { root = NULL;}// DestructorTree::~Tree() { freeNode(root);}// Free the nodevoid Tree::freeNode(Node* leaf){ if ( leaf != NULL ) { freeNode(leaf->Left()); freeNode(leaf->Right()); delete leaf; }}// Add a nodevoid Tree::addNode(int key) { // No elements. Add the root if ( root == NULL ) { cout Left()); cout Left()); postOrder(n->Right()); cout addNode(10); tree->addNode(20); tree->addNode(40); tree->addNode(50); coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.