二叉搜索树(BST)

时间:2012-12-23 15:26:00

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

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

谢谢:))

我的算法

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

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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");
}

答案 2 :(得分: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);
}