0

我有这个简单的问题。在我做的主要功能中:bsearch_tree bs1;我在 BST 树上宣布。然后我向那棵树添加节点。然后我在另一个 BST 树上宣布:bsearch_tree bs2;我再次向它添加节点(不同)。

它的作用是将相同的节点添加到树 bs1 和 bs2 中。因为,当我打印两棵树时,我发现它们是相同的,但它们不应该是相同的。这是错的。

我尝试调试,发现当我添加节点时,它会将它们添加到同一棵树中。

我不知何故需要使树木与众不同。

我想我必须创建该类的新实例,bsearch_tree但我不能更改 Main 函数(限制)。

这是头文件:

#include<iostream>

using namespace std;

class tnode
{
private:
    double data;
    tnode *left;
    tnode *right;
public:
    tnode(double key);
    friend class bsearch_tree;
    friend void assist(tnode *tmp);
    friend ostream& operator<<(ostream& stream,tnode const *root);
};
class bsearch_tree
{
    tnode *root;
public:
    bsearch_tree();
    bsearch_tree(int);
    void duplicate_tree(tnode *root,tnode **new_root);
    void free_tree(tnode *node);
    void add_value(double v);
    tnode copy(tnode *root);
    bsearch_tree operator + (const bsearch_tree & t);
    bsearch_tree & operator=(const bsearch_tree & v);
    friend void print(bsearch_tree tree);
    friend ostream& operator<<(ostream& stream,bsearch_tree const root);
private:
    void AddNode(double key,tnode *leaf);
    void SumTree(tnode *tree);
};

这是代码文件:

#include<iostream>
#include "head.h"

using namespace std;

tnode::tnode(double key)
{
    this->data = key;
    this->left = NULL;
    this->right = NULL;
}

bsearch_tree::bsearch_tree()
{
    bsearch_tree *root = new bsearch_tree(NULL);
    //root = NULL;
}

bsearch_tree::bsearch_tree(int)
{
    //bsearch_tree *root = new bsearch_tree(NULL);
    root = NULL;
}

void bsearch_tree::duplicate_tree(tnode *root,tnode **new_root)
{
    if(root == NULL)
        return;
    (**new_root) = copy(root);
}

tnode bsearch_tree::copy(tnode *root)
{
    tnode* node = new tnode(root->data);
    if(root == NULL)
        return *node;
    copy(root->left);
    copy(root->right);
    return *node;
}

void bsearch_tree::free_tree(tnode *node)
{
    if(node == NULL)
        return;
    free_tree(node->left);
    free_tree(node->right);
    free(node);
}

void bsearch_tree::add_value(double v)
{
    tnode *tmp;
    if(root == NULL)
    {
        tmp = new tnode(v);
        root = tmp;
    }
    else
        AddNode(v,root);
}
void bsearch_tree::AddNode(double key,tnode *leaf)
{
    if(key >= leaf->data)
    {
        if(leaf->right != NULL)
            AddNode(key,leaf->right);
        else
        {
            tnode * n = new tnode(key);
            leaf->right = n;
        }
    }
    else
    {
        if(leaf->left != NULL)
            AddNode(key,leaf->left);
        else
        {
            tnode * n = new tnode(key);
            leaf->left = n;
        }
    }
}

void print(bsearch_tree tree)
{
    assist(tree.root);
}

void assist(tnode *tmp)
{
    if(tmp)
    {
        assist(tmp->left);
        cout << tmp->data << endl;
        assist(tmp->right);
    }
}

ostream & operator << (ostream & stream,bsearch_tree const root)
{
    print(root);    
    return stream;
}


bsearch_tree bsearch_tree::operator + (const bsearch_tree & t)
{
    bsearch_tree bs;
    SumTree(t.root);
    return *this;
}

void bsearch_tree::SumTree(tnode *tree)
{
    if(tree)
    {
        AddNode(tree->data,root);
        SumTree(tree->left);
        SumTree(tree->right);
    }
}

bsearch_tree & bsearch_tree::operator = (const bsearch_tree & v)
{
    if(this == &v)
        return *this;

    root = v.root;
    return *this;
}

int main()
{
  bsearch_tree bs1;

  bs1.add_value(16.0);  
  bs1.add_value(14.0);  
  bs1.add_value(6.0);  
  bs1.add_value(18.0);  
  bs1.add_value(17.0);  
  bs1.add_value(4.0);  
  bs1.add_value(5.0);  
  bs1.add_value(1.0);  
  bs1.add_value(26.0);  
  bs1.add_value(22.0);  

  cout << "bs1:" << endl;
  print(bs1);

  bsearch_tree bs2;

  bs1 = bs1 = bs1;
  bs2 = bs1;

  bs2.add_value(40.0);
  bs2.add_value(20.0);
  bs2.add_value(60.0);

  cout << "cout << bs2" << endl;
  cout << bs2;

  bsearch_tree bs3;

  //bs3 = bs1 + bs2;
  bs3 = bs1;

  cout << "cout << bs3" << endl;
  cout << bs3; 
  cin.get();
  return 0;
} // main

所以我的问题是:在 main 函数中,我做bsearch_tree bs1;了一些节点,然后我添加了一些节点,如下所示:bs1.add_value(16.0);然后我这样做bsearch_tree bs2;,我再次添加了一些节点。然后我打印两个树 bs1 和 bs2 ,我发现它们相等,这是不对的。

如何制作add_value将节点添加到不同树的方法?因此,当我打印两棵树时,我会得到不同的树,因为我输入的节点一开始就不同。

谢谢

4

1 回答 1

0

operator=的不正确。您正在设置一个根以指向第二棵树的同一根。因此,从使用此运算符(例如,in bs2 = bs1;)的角度来看,您的两个树都处理相同的共享数据。您需要以创建树数据副本的方式实现赋值运算符。

于 2012-12-28T08:59:25.310 回答