gpt4 book ai didi

c++ - 类树与结构树节点

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:30 25 4
gpt4 key购买 nike

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

"Finish the implementation of the createExpressionTree() method"

还有那个

"Its very similar to your previous implementation, except that you will be using instances of the "class Tree" instead of instances of the "structure TreeNode"."

两者的内容比下面的要多,但我认为您可以了解事情的要点,所以我要问的是:类实现与结构实现有多相似?我可以只复制并粘贴下面显示的旧代码并做一些小的调整吗?我一直在尝试,但在访问私有(private)成员等方面遇到问题。

所以这是类树和我应该与之一起使用的新版本的 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;

上一个创建表达式树

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;
}

}


}

最佳答案

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

如果他只是想让你把“struct”这个词改成“class”,那么你可以使用访问修饰符“public”来访问leftPtr/rightPtr

例子:

struct blah
{
int boringNumbers;
string boringStrings;
};

等同于:

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

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

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

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

关于c++ - 类树与结构树节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10458270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com