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

3 回答 3

3

你按价值传递struct node *k。每当您在函数中更改它(如 in 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);
于 2012-12-23T15:35:16.653 回答
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");
}
于 2012-12-23T15:37:49.610 回答
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);
}
于 2012-12-23T15:51:59.307 回答