树不是建筑

Tree not builiding

本文关键字:建筑      更新时间:2023-10-16

我试图建立自己的二叉搜索树。然而,我的树没有建起来。请参阅下面的代码和帮助。

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

class Binarytree 
{  
private:
    struct node
    { 
      int data;
      node *left;
      node *right;
    };
    node  *root;

public:
    Binarytree();
    void insertdata(node*,int);
    void deletedata();
    void printdata(node*);
    void userprint(char);
    void getdata(int);
};
Binarytree::Binarytree()
{
    root=NULL;
    cout<<"Setting root as NULL"<<endl;
}
void Binarytree::insertdata(node* temp3,int temp)
{   cout << "in insert data"<<endl;
    node Dummy=node();
    Dummy.data=temp;
    Dummy.left=NULL;
    Dummy.right=NULL;

    cout << "Data To be inserted is  "<<temp <<endl;
    if  (temp3 == NULL)
      {  cout<<"Found NULL ROOT"<<endl; 
          temp3=&Dummy;
          cout << "Entered a Data in tree"<<endl;
          cout<<"Data in root"<<root->data<<endl;
    }

    else if (temp3->data > temp)
    { cout<<"Making a Left Recursive Call"<<endl;
     insertdata(temp3->left,temp);
    }
    else 
    { cout<<"Making a right  Recursive Call"<<endl;
        insertdata(temp3->right,temp);
    }

  }
void Binarytree::getdata(int check)
{   cout <<"in getdata"<<endl;
    cout << "before insertdata"<<endl;
    insertdata(root,check);
}
void Binarytree::printdata(node* printrt)
{
    if (printrt ==NULL)
         cout << "Nothing to print";
    else
        { cout << printrt->data << endl;
          printdata(printrt->left);
          printdata(printrt->right);
                   }
}
void Binarytree::userprint(char in)
{   node* data;
    data=root;
    if (in == 'Y' || in == 'y')
      printdata(data);
}
void main()
{    Binarytree element=Binarytree();
     int userdata,i=0;
     bool check = true;

     while(check)
    { cout <<"Please Enter your Data"<<endl;
     cin >> userdata;
     element.getdata(userdata);
     cout<<"FUnction returned to main"<<endl;
     i++;
     if(i==5)
         check=false;
     }
     element.userprint('Y');
}

第一个值没有插入根指针。我知道有很多代码可以做到这一点,但如果我不自己编码,我觉得我的学习将是有限的。所以请帮忙找出这段代码中的错误

已经真正尝试编译,可能有其他问题…但

改变
void Binarytree::insertdata(node* temp3,int temp)

void Binarytree::insertdata(node* &temp3,int temp)

以便在insertdata内部创建的节点真正修改外部指针。

和改变

node Dummy=node();
Dummy.data=temp;
Dummy.left=NULL;
Dummy.right=NULL;

node *Dummy=new node();
Dummy->data=temp;
Dummy->left=NULL;
Dummy->right=NULL;
正如我所说的,可能还有其他问题……你应该担心删除节点和所有这些…

或者您可以在insertdata()之外创建节点并保持相同的签名。

祝你好运

根本原因(请原谅这个双关语)是您向树的根添加东西的方式。您的代码在堆栈上创建一个名为Dummy临时变量,然后获取其地址。这是第一个错误,因为当函数结束时,临时变量将被销毁。

第二个问题是,为了改变传递给函数的指针的值,必须将指针传递给另一个指针。换句话说,如果你想真正改变传递的指针,而不是像原始代码那样只做一个局部复制,那么你的成员函数insertdata(node *, int)必须变成insertdata(node **, int)

为了说明这个事实,试试下面的代码。

#include <iostream>
int Y = 99;
void makeitsix(int n) {
    n = 6;
}
void pointToY(int *ptr) {
    ptr = &Y;
}
int main()
{
    int x = 5;
    int *p = &x;
    std::cout << "x = " << x << ", *p = " << *p << std::endl;
    makeitsix(x);
    pointToY(p);
    std::cout << "x = " << x << ", *p = " << *p << std::endl;
    return 0;
}  

makeitsix()被调用时,它只是n的本地副本被改变,而不是最初传入的5的值。类似地,pointToY()函数中的ptr只修改了ptr的本地副本,而不是用于从main()内部调用该函数的p。如果不是这样,像makeitsix(3)这样的调用将导致一些非常奇怪的效果!

我冒昧地在你的代码中做了一些修改,使它更简洁一些,包括

  1. 赋予node结构自己的构造函数
  2. 为Binarytree创建提取器
  3. 删除各种诊断打印输出语句(为了简洁)
  4. 使树的打印输出看起来更像树,根在左边,分支向右延伸
  5. 将一些成员函数设为私有

和其他一些次要的东西。完整的工作代码如下:

#include<iostream>
#include<string>
class Binarytree 
{  
private:
    struct node
    { 
      node(int d=0) : data(d), left(NULL), right(NULL) {};
      int data;
      node *left;
      node *right;
    };
    node  *root;
    void insertdata(node**,int);
    std::ostream& printdata(std::ostream &out, node*, int depth=0);
public:
    Binarytree() : root(NULL) {};
    std::ostream &printTo(std::ostream &out);
    void insert(int);
};
void Binarytree::insertdata(node** temp3,int temp)
{   
    node *Dummy=new node(temp);
    if  (*temp3 == NULL) {  
        *temp3=Dummy;
    } else if ((*temp3)->data > temp) { 
        insertdata(&((*temp3)->left),temp);
    } else { 
        insertdata(&((*temp3)->right),temp);
    }
}
void Binarytree::insert(int check)
{   
    insertdata(&root,check);
}
std::ostream &Binarytree::printdata(std::ostream &out, node* printrt, int depth)
{
    if (printrt != NULL)
    { 
        printdata(out, printrt->left, depth+1);
        for (int i = 0; i < depth; ++i) 
            out << 't';
        out << printrt->data << std::endl;
        printdata(out, printrt->right, depth+1);
    }
    return out;
}
std::ostream &Binarytree::printTo(std::ostream &out)
{   
      return printdata(out, root);
}
std::ostream &operator<<(std::ostream &out, Binarytree &b)
{
    return b.printTo(out);
}
int main()
{    
    Binarytree element;
    int userdata,i=0;
    bool check = true;
    while(check)
    { 
        std::cout << "Please Enter your Data" << std::endl;
        std::cin >> userdata;
        element.insert(userdata);
        i++;
        if(i==5)
            check=false;
    }
    std::cout << "Tree:n" << element << std::endl;
    return 0;
}