【问题标题】:Binary Search Tree(BST) [closed]二叉搜索树(BST)[关闭]
【发布时间】:2012-12-23 15:26:00
【问题描述】:

大家好,我犯了逻辑错误,但我没有发现错误。

谢谢你:))

我的算法

#include <iostream>   //iostream

using namespace std;

struct node{

    struct node *left;
    struct node *right;
    int data;
};


void add(node *p,int sayi){

    if(p==NULL){
        p=new node();
        p->data=sayi;
        p->left=NULL;
        p->right=NULL;

    }
    else if(p->data>=sayi){
            add(p->left,sayi);  
    }
    else    {
            add(p->right,sayi);
    }

}

void postorder(node *p)
{

if(p!=NULL)

    {
        if(p->left!=NULL)
            postorder(p->left);
        if(p->right!=NULL)
            postorder(p->right);
        cout<< p->data<<endl;
    }
    else{
        cout<<"hata"<<endl;

    }
}

  void main(){

    struct node *k=NULL ;
    int sayi=0;

    while(sayi!=-1){
    cout<<"Bir sayi giriniz...";
    cin>>sayi;
    add(k,sayi);
    }
    postorder(k);

    system("pause");
}

【问题讨论】:

  • 你在main 的循环中使用sayi 做什么?你的输出是什么?
  • 我想获取用户数量并创建 bst。说是数字。
  • 别忘了delete所有这些newnodes!
  • 五个赞成票?认真的吗?

标签: c++ tree binary-search-tree


【解决方案1】:

您通过值传递struct node *k。每当您在函数中更改它(如在add 中)时,它只会更改本地副本(在函数中),因此您会返回一个NULL 指针。通过引用或指针传递它:

void add(node* &p,int sayi)
{
     ...
}

struct node *k = 0;
...
add(k);

void add(node** p,int sayi)
{
     ... 
}

struct node *k = 0;
...
add(&k);

【讨论】:

  • 你有别的解决办法吗
  • 通过引用传递是最简单的解决方案。你为什么想要另一个@user1925149?
  • 我问我想知道什么:)
【解决方案2】:

您的数据结构应该有一个根节点来跟踪。您需要将此根节点引用传递给您的postorder()add() 函数调用。这里k 看起来是你的根节点。在外面声明k,这样就可以在函数add()里面访问了。

#include <iostream>   //iostream

using namespace std;

struct node{

    struct node *left;
    struct node *right;
    int data;
};

struct node *k=NULL; //ROOT NODE


void add(node *p,int sayi){

    if(p==NULL){
        p=new node();
        p->data=sayi;
        p->left=NULL;
        p->right=NULL;
        if(k==NULL)
        k=p;  //When the first node is created, we assign it to root, i.e, k    
    }
    else if(p->data>=sayi){
            add(p->left,sayi);  
    }
    else    {
            add(p->right,sayi);
    }

}

void postorder(node *p)
{

if(p!=NULL)

    {
        if(p->left!=NULL)
            postorder(p->left);
        if(p->right!=NULL)
            postorder(p->right);
        cout<< p->data<<endl;
    }
    else{
        cout<<"hata"<<endl;

    }
}

  void main(){

    int sayi=0;

    while(sayi!=-1){
    cout<<"Bir sayi giriniz...";
    cin>>sayi;
    add(k,sayi);
    }
    postorder(k);

    system("pause");
}

【讨论】:

    【解决方案3】:

    尝试将您的代码更改为:

    #include <iostream>   //iostream
    
    using namespace std;
    
    struct node{
    
        struct node *left;
        struct node *right;
        int data;
    };
    
    
    node* add(node *p,int sayi){
    
        if(p==NULL){
            p=new node();
            p->data=sayi;
            p->left=NULL;
            p->right=NULL;
            return p;
        }
        else if(p->data>=sayi){
                p->left=add(p->left,sayi);  
        }
        else    {
                p->left=add(p->right,sayi);
        }
        return p;
    }
    
    void postorder(node *p)
    {
    
    if(p!=NULL)
    
        {
            if(p->left!=NULL)
                postorder(p->left);
            if(p->right!=NULL)
                postorder(p->right);
            cout<< p->data<<endl;
        }
        else{
            cout<<"hata"<<endl;
    
        }
    }
    
    int main(){
    
        struct node *k=NULL ;
        int sayi=0;
    
        while(sayi!=-1){
        cout<<"Bir sayi giriniz...";
        cin>>sayi;
        k=add(k,sayi);
        }
        postorder(k);
    }
    

    【讨论】:

    • 谢谢rondogiannis。这段代码有时会写错,但这很好。
    • @user1925149 不客气!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 2012-05-13
    • 2013-01-08
    • 2020-06-11
    • 2019-05-06
    • 1970-01-01
    • 2020-09-03
    相关资源
    最近更新 更多