重载赋值运算符以在后缀表达式解析器树C ++中使用

时间:2013-09-23 19:26:53

标签: parsing tree operator-overloading overloading variable-assignment

前几天我对同一个节目提出了一个问题,但现在我遇到了一个新问题。我的讲师告诉我需要重载我的赋值运算符,以允许我的构造函数理解将一个二叉树分配给另一个。我已经尝试了几种不同的努力,但似乎无法获得正确的语法或想法。我花了一个小时在谷歌上挖掘,似乎找不到任何足够接近我正在做的事情来帮助我。他说话的方式似乎让运营商过载就足够了。在线的每个例子似乎都使用重载和单独的功能。有什么想法吗?

这是我到目前为止所拥有的:

#ifndef BINARYTREE_H
#define BINARYTREE_H

using namespace std;
#include <cstdlib>
#include <iostream>

template <class Node_Type>
class BinaryTree 
{
public:
    BinaryTree();
    BinaryTree(Node_Type);
    BinaryTree(Node_Type, BinaryTree<Node_Type>, BinaryTree<Node_Type>);
    bool isEmpty();
    Node_Type info();
    Node_Type inOrder();
    Node_Type preOrder();
    Node_Type postOrder();
    const BinaryTree & operator=(const BinaryTree<Node_Type> & original);

private:
    struct Tree_Node
{
    Node_Type Node_Info;
    BinaryTree<Node_Type> *left;
    BinaryTree<Node_Type> *right;
};

Tree_Node *root;

};

template <class Node_Type>
BinaryTree<Node_Type>::BinaryTree() {

    root = NULL;

}

template <class Node_Type>
BinaryTree<Node_Type>::BinaryTree(Node_Type rootNode) {

    root = new Tree_Node;
    root->Node_Info = rootNode;
    root->left = NULL;
    root->right = NULL;

}

template <class Node_Type>
BinaryTree<Node_Type>::BinaryTree(Node_Type rootNode, BinaryTree<Node_Type> leftTree,                 BinaryTree<Node_Type> rightTree){

    root = new Tree_Node;
    root->Node_Info = rootNode;

    root->left =  &leftTree;
    root->right =  &rightTree;

}

template <class Node_Type>
bool BinaryTree<Node_Type>::isEmpty(){

    if (root == NULL)
    return true;   
}

template <class Node_Type>
Node_Type BinaryTree<Node_Type>::info(){

        return root->Node_Info;

}

template <class Node_Type>
Node_Type BinaryTree<Node_Type>::inOrder(){

    if (root->left != NULL)
        root->left->inOrder();
    cout << root->Node_Info;
    if (root->right != NULL)
        root->right->inOrder();

}

template <class Node_Type>
Node_Type BinaryTree<Node_Type>::preOrder(){

    cout << root->Node_Info;
    if (root->left != NULL)
        root->left->preOrder();
    if (root->right != NULL)
        root->right->preOrder();

}

template <class Node_Type>
Node_Type BinaryTree<Node_Type>::postOrder(){


    if (root->left != NULL)
        root->left->postOrder();
    if (root->right != NULL)
        root->right->postOrder();
    cout << root->Node_Info;
}

template <class Node_Type>
const BinaryTree<Node_Type> & BinaryTree<Node_Type>::operator =(const BinaryTree<Node_Type>& original){

    root = new Tree_Node;
    root->Node_Info = original.info();

    root->left = original->root->left;
    root->right = original->root->right;

    return *this;

}


#endif  /* BINARY_TREE_H */

我知道这里可能存在根本性的错误。我只是不太了解C ++来真正解决问题。上学期我用指针和动态记忆的经验有限。对不起,如果我严重搞砸了。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

您的分配覆盖是浅拷贝的定义:

root->left = original->root->left;
root->right = original->root->right;

深层复制将在整个树中创建新节点。 (需要遍历)

你有&#34;树&#34;在每个节点级别下,您已经得到了&#34; tree-&gt; left-&gt; tree-&gt; right-&gt; tree-&gt; right&#34;而不只是&#34; root-&gt; left-&gt; right-&gt; right&#34;。这会让我很快感到困惑。