I need to finish implementing the rest of the program, with the parts called \"f
ID: 3562554 • Letter: I
Question
I need to finish implementing the rest of the program, with the parts called "fill this part", a little lost, please help..
#include <iostream>
#include <string>
#include <stack>
using namespace std;
class Node{
private:
char value;
Node* lchild;
Node* rchild;
public:
Node() { value = ' '; lchild = rchild = NULL; }
Node(char val) { value = val; lchild = rchild = NULL; }
~Node() { delete lchild; delete rchild; }
void insertLChild(Node* node) { lchild = node; }
void insertRChild(Node* node) { rchild = node; }
char getValue () { return value; }
Node* getLChild () { if (lchild == NULL) return NULL; return lchild; }
Node* getRChild () { if (rchild == NULL) return NULL; return rchild; }
};
void printInorder(Node* root) {
// FILL THIS PART
if (root == NULL) return;
printInorder(root->getLChild());
cout << root->getValue() << endl;
printInorder(root->getRChild());
}
void printPreorder(Node* root) {
// FILL THIS PART
if (root == NULL) return;
cout << root->getValue() << endl;
printPreorder(root->getLChild());
printPreorder(root->getRChild());
}
void printPostorder(Node* root) {
// FILL THIS PART
if (root == NULL) return;
printPostorder(root->getLChild());
printPostorder(root->getRChild());
cout << root->getValue() << endl;
}
Node * createTree(string expression) {
stack<Node*> treeStack;
for (char c: expression) {
// FILL THIS PART
// FILL THIS PART
}
Node * tree = treeStack.top();
treeStack.pop();
return tree;
}
int main() {
cout << "please, enter a mathematical expression" << endl;
string expression;
cin >> expression;
Node * tree = createTree(expression);
cout << "printing inorder" << endl;
printInorder (tree);
cout << "---------------------" << endl;
cout << "printing preorder" << endl;
printPreorder (tree);
cout << "---------------------" << endl;
cout << "printing postorder" << endl;
printPostorder (tree);
cout << "---------------------" << endl;
delete tree;
}
Explanation / Answer
OUTPUT:-