Plese help me with a c++ program about expressiontree. Implement class Expressio
ID: 3766589 • Letter: P
Question
Plese help me with a c++ program about expressiontree.
Implement class ExpressionTree as it is defined in express.h. Your implementation must be in a new file that you create named express.cpp.
This variation adds three print methods to the problem presented in the text.
Implement each of the member functions (including both constructors, the destructor and the assignment operator) of class ExpressionTest.
Be sure to #include "express.h".
The print functions must show the expression parts (numbers and operators and parentheses) separated by one space from each other, and there must be one blank space at the end of each printed expression. Print to cout.
Function printPre must print the expression in pre-order form, and printPost must print it in post-order form. Function printIn must print parentheses surrounding each addition operation, but multiplication operations must not be parenthesized, and of course this function must print the expression in in-order form.
You may use expresstest.cpp for that purpose. The program will run one of nine tests, and here are sample results from our solution, showing each of the nine tests being run.
Here is the express.h
And here is the expresstest.cpp
And here is the sample results.
Thanks for any help!
Explanation / Answer
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node *left;
Node *right;
Node()
{
data = 0;
left = NULL;
right = NULL;
}
};
class ExpressionTree
{
Node *root;
void insert(int d, Node *node)
{
if (d < node->data)
{
if (node->left == NULL)
{
Node *leaf = new Node();
leaf->data = d;
node->left = leaf;
}
else
{
insert(d, node->left);
}
}
else
{
if (node->right == NULL)
{
Node *leaf = new Node();
leaf->data = d;
node->right = leaf;
}
else
{
insert(d, node->right);
}
}
}
void inOrderDisplay(Node *subRoot)
{
if (subRoot != NULL)
{
inOrderDisplay(subRoot->left);
cout << subRoot->data << " ";
inOrderDisplay(subRoot->right);
}
}
void postOrderDisplay(Node *subRoot)
{
if (subRoot != NULL)
{
postOrderDisplay(subRoot->left);
postOrderDisplay(subRoot->right);
cout << subRoot->data << " ";
}
}
void preOrderDisplay(Node *subRoot)
{
if (subRoot != NULL)
{
cout << subRoot->data << " ";
preOrderDisplay(subRoot->left);
preOrderDisplay(subRoot->right);
}
}
void deleteSubtree(Node *subRoot)
{
if (subRoot != NULL)
{
deleteSubtree(subRoot->left);
deleteSubtree(subRoot->right);
cout << " deleting: " << subRoot->data;
delete subRoot;
subRoot = NULL;
}
}
public:
ExpressionTree()
{
root = NULL;
}
~Tree()
{
deleteAll();
}
void insert(int d)
{
if (root == NULL)
{
Node *leaf = new Node();
leaf->data = d;
root = leaf;
}
else
{
insert(d, root);
}
}
void inOrderDisplay()
{
inOrderDisplay(root);
}
void postOrderDisplay()
{
postOrderDisplay(root);
}
void preOrderDisplay()
{
preOrderDisplay(root);
}
void deleteAll()
{
deleteSubtree(root);
}
};
.Main Class:
#include<iostream>
#include"task1.h"
using namespace std;
void main()
{
ExpressionTree tree;
tree.insert(10);
tree.insert(6);
tree.insert(14);
tree.insert(5);
tree.insert(8);
tree.insert(11);
tree.insert(18);
cout << endl;
system("pause");
}