【问题标题】:Nodes do not attach to tree节点不附加到树
【发布时间】:2016-07-19 06:44:53
【问题描述】:

我正在实现一个二叉树来存储 C++ 中的算术表达式。但是,我在向树中插入节点时遇到了一些麻烦。我想通过解析元素和地址将节点插入树。

它崩溃了,可能是因为节点没有附加到树上。

希望你们能帮我解决这个问题。欢迎提出其他实现二叉树来存储算术表达式的方法的想法。

这是我的程序:

struct Node{
    int element;
    Node* left;
    Node* right;

    Node(int e){
        this->element = e;
        this->left = NULL;
        this->right = NULL;
    }

    bool isInternal(){
        return (this->left != NULL || this->right != NULL);
    }

    void print(){
        if(isInternal()){
            switch(this->element){
            case 1:
                cout << " + ";
                break;
            case 2:
                cout << " - ";
                break;
            case 3:
                cout << " * ";
                break;
            case 4:
                cout << " / ";
                break;
            }
        }
        else
            cout << this->element;
    }

    bool hasLeft(){
        return (this->left != NULL);
    }

    bool hasRight(){
        return (this->right != NULL);
    }
};

class BinaryTree{
public:
    Node* root;

    void clearTree(Node* t){
        if(t == NULL)
            return;
        if(t->left != NULL)
            clearTree(t->left);
        if(t->right != NULL)
            clearTree(t->right);
        delete t;
        return;
    }

    BinaryTree(){
        root = NULL;
    }

    ~BinaryTree(){
        clearTree(root);
    }

    bool isEmpty(){
        return (root == NULL);
    }

    Node* insertNode(int e, Node* node){
        Node* newNode = new Node(e);
        node = newNode;
        return node;
    }

    void printExpression(Node* node){
        if(node->hasLeft()){
            cout << "(";
            printExpression(node->left);
        }
        node->print();
        if(node->hasRight()){
            printExpression(node->right);
            cout << ")";
        }
    }
};
int main(){
    BinaryTree* bt = new BinaryTree();

    Node* root = bt->root;
    bt->insertNode(1, root);
    Node* n1 = bt->insertNode(3, root->left);
    bt->insertNode(2, n1->left);
    Node* n2 = bt->insertNode(2, n1->right);
    bt->insertNode(4, n2->left);
    bt->insertNode(1, n2->right);
    Node* n3 = bt->insertNode(3, root->right);
    bt->insertNode(3, n3->left);
    bt->insertNode(5, n3->right);

    bt->printExpression(root);

    return 0;
}

【问题讨论】:

    标签: c++ binary-tree


    【解决方案1】:
    Node* insertNode(int e, Node* node){
        Node* newNode = new Node(e);
        node = newNode;
        return node;
    }
    

    您将一个名为“node”的参数传递给您的“insertNode()”方法。您创建一个新节点,并将其分配给参数。

    这完全没有任何作用。所以呢?此函数返回后,您对其参数所做的操作将完全忘记。只是遥远的记忆。

    从您调用insertNode() 方法的方式来看,您可能打算通过引用而不是通过值来传递此参数。

    【讨论】:

    • 是的,我想通过引用传递参数。但我真的不知道怎么做。我以为传递指针就是传递引用,但是这次它不起作用。
    • Node* insertNode(int e, Node* &amp;node) {...
    • 获得一本关于 C++ 的好书。它将解释什么是引用,以及按值传递和按引用传递之间的区别。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多