0

数日前にこの同じプログラムについて質問がありましたが、今は新しい問題に行き詰まっています。私のインストラクターは、コンストラクターが 1 つのバイナリ ツリーを別のバイナリ ツリーに割り当てることを理解できるようにするには、割り当て演算子をオーバーロードする必要があると言いました。私はいくつかの異なる取り組みを試みましたが、正しい構文やアイデアを理解できないようです。Google で 1 時間かけて調べてみましたが、実際に私が行っていることに十分に近い例を見つけることができないようです。彼の話し方は、オペレーターに過負荷をかけるだけで十分であるかのように見えました。オンラインのすべての例は、オーバーロードと別の関数を使用しているようです。何かご意見は?

これが私がこれまでに持っているものです:

#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++ について十分な知識がなく、問題を解決することができません。私は、ポインターと動的メモリに関して、前学期の経験が限られていました。私が物事をひどく台無しにしていたら、ごめんなさい。助けてくれてありがとう!

4

1 に答える 1

0

割り当てのオーバーライドは、浅いコピーの定義です。

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

ディープ コピーは、ツリー全体に新しいノードを作成します。(トラバーサルが必要)

また、ノードの各レベルの下に「ツリー」があると混乱しているように見えるため、「ルート->左->右-」ではなく「ツリー->左->ツリー->右->ツリー->右」となります。 >そうですね」。それは私をすぐに混乱させるでしょう。

于 2013-09-23T21:26:32.690 に答える