类树与结构TreeNode

时间:2012-05-05 02:16:04

标签: c++ class tree

我有两个独立的程序,都处理用中缀和后缀评估表达式树。一个基于结构,而另一个基于类。现在我被困在我的任务部分(对于课程版本)说:

  

“完成createExpressionTree()方法的实现”

那个

  

“它与您之前的实现非常相似,除了您将使用”类树“的实例而不是”结构TreeNode“的实例。”

除了以下内容之外,还有更多内容,但我认为你可以得到事情的要点,所以我要问的是:类结构的类实现有多相似?我可以复制并粘贴下面显示的旧代码并进行微调吗?我一直在努力,但在访问私人会员等方面存在问题。

所以这里是类树和我的新版本的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;
    }

}


}

1 个答案:

答案 0 :(得分:1)

我认为类和结构版本之间没有任何区别(结构和类之间的唯一区别是默认情况下类的数据是私有的)。

如果他只想让你将“struct”改为“class”,那么你可以使用访问修饰符“public”访问leftPtr / rightPtr

示例:

struct blah
{
  int boringNumbers;
  string boringStrings;
};

与:

相同
class blah2
{
public:
  int boringNumbers;
  string boringStrings;
};

但是,他可能还希望您设计应用程序以将更多代码移入类中。

这至少包括将char_is_operator等函数移动到类中。

我对此表示怀疑,但他/她可能还希望您创建节点层次结构,这样您就不需要将数据存储为通用字符串。