我有两个独立的程序,都处理带有中缀和后缀的表达式树。一种基于结构,另一种基于类。现在我被困在我的作业部分(对于课堂版本)说:
“完成 createExpressionTree() 方法的实现”
然后
“它与您之前的实现非常相似,只是您将使用“类树”的实例而不是“结构树节点”的实例。”
两者都比下面的内容更多,但我认为您可以了解事情的要点,所以我要问的是:类实现与结构一有多相似?我可以复制并粘贴下面显示的旧代码并进行微调吗?我一直在尝试,但在访问私人成员等方面存在问题。
所以这是类树和我应该与之配套的新版本 createExpressionTree
#ifndef TREE_H
#define TREE_H
#include <vector>
#include <stack>
#include <sstream>
#include <map>
# define TYPE_NUMBER 0
# define TYPE_VARIABLE 1
# define TYPE_OPERATOR 2
class Tree
{
public:
Tree(std::string input,Tree *leftSubTree=NULL,Tree *rightSubTree=NULL);
Tree(const Tree &inTree); //COPY CONSTRUCTOR
~Tree(); //DESTRUCTOR
int evaluate(std::map< std::string, int > ipMap); //EVALUATE THE EXPRESSION
void postOrderPrint();
void inOrderPrint();
private:
Tree *leftPtr;
std::string Op;
Tree *rightPtr;
int NodeType;
};
与树类关联的代码
Tree::Tree(std::string input,Tree *leftSubTree,Tree *rightSubTree){
Op = input;
leftPtr = leftSubTree;
rightPtr = rightSubTree;
int num;
if (input == "+"|input == "-"|input == "*"|input == "/")
NodeType = TYPE_OPERATOR;
else if(std::istringstream(Op)>>num)
NodeType = TYPE_NUMBER;
else
NodeType = TYPE_VARIABLE;
}
// copy constructor
Tree::Tree(const Tree &inTree){
Op = inTree.Op;
NodeType = inTree.NodeType;
if (inTree.leftPtr == NULL){
leftPtr = NULL;
}
else {
leftPtr = new Tree(*(inTree.leftPtr));
}
if (inTree.rightPtr == NULL){
rightPtr = NULL;
}
else {
rightPtr = new Tree(*(inTree.rightPtr));
}
}
// tree destructor
Tree::~Tree(){
std::cout << "Tree destructor called" << std::endl;
if (leftPtr != NULL) {
delete(leftPtr);
leftPtr = NULL;
}
if (rightPtr != NULL) {
delete(rightPtr);
rightPtr = NULL;
}
}
#endif
新的 createExpressionTree 我希望得到一些帮助:
void arithmetic_expression::createExpressionTree(std::vector<std::string> expression)
{
std::stack <Tree> localStack;
std::string Op;
//Very similar to old implementation
}
并且这里是之前实现的结构treeNode和之前完成的createExpressionTree
struct treeNode {
treeNode *leftPtr; /* pointer to left subtree */
std::string Op; /* integer data value */
treeNode *rightPtr; /* pointer to right subtree */
};
typedef struct treeNode TreeNode;
typedef TreeNode * TreeNodePtr;
以前的 createExpressionTree
void arithmetic_expression::createExpressionTree(std::vector<std::string> expression)
{
std::stack <TreeNodePtr> localStack;
std::string Op;
TreeNodePtr ptr;
for(int i=0; i<expression.size();i++)
{
Op = expression[i];
ptr = createNewTreeNode(Op);
if(char_is_operator(Op))
{
// adding element to right tree
if (localStack.empty())
{
std::cout<< "Invalid expression: tree not created " << std::endl;
topPtr = NULL;
return;
}
else
{
ptr->rightPtr = localStack.top();
localStack.pop();
}
// adding element to left tree
if (localStack.empty()) {
std::cout<< "Invalid expression: tree not created " << std::endl;
topPtr = NULL;
return;
}
else
{
ptr->leftPtr = localStack.top();
localStack.pop();
}
}
// pushing element to stack
localStack.push(ptr);
}
if (localStack.empty()) {
std::cout<< "Invalid expression: tree not created " << std::endl;
topPtr = NULL;
}
else
{
topPtr = localStack.top();
localStack.pop();
if (!localStack.empty()) {
std::cout<< "Invalid expression: tree not created " << std::endl;
topPtr = NULL;
}
}
}