二叉搜索树(BST)

3

大家好,我犯了一个逻辑错误,但我找不到错误所在。

谢谢:))

我的算法:

#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 做了什么?你的输出是什么? - Mene
我想获取用户数量并创建二叉搜索树。sayi表示数字。 - cengiz
不要忘记删除所有这些新的节点! - johnsyweb
1
五个赞?认真的吗? - Lightness Races in Orbit
3个回答

3

您将 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);

你有其他的解决方案吗? - cengiz
传递引用是最简单的解决方案。为什么你还想要另一种方式呢,@user1925149? - johnsyweb
我问了我想知道的 :) - cengiz

2

您需要一个根节点来跟踪您的数据结构。您需要将此根节点引用传递给您的postorder()add()函数调用。这里k看起来是您的根节点。在函数add()内部可以访问k,因此请在外部声明k

#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");
}

2

尝试将你的代码更改为以下内容:

#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。这段代码有时会写入不正确,但这非常好。 - cengiz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接