2012-12-23 93 views
3

大家好,我犯了逻辑错误,但我不认为有错。二叉搜索树(BST)

谢谢:))

我的算法

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

什么是你用'sayi'在'main'循环做什么?你的输出是什么? – Mene

+0

我想获得用户数量并创建bst。 sayi平均数。 – cengiz

+0

不要忘记'删除'所有这些“新”节点! – Johnsyweb

回答

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); 
+0

你有另一种解决方案 – cengiz

+0

通过引用传递是最简单的解决方案。你为什么要另一个@ user1925149? – Johnsyweb

+0

我问我想知道什么:) – cengiz

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

试着改变你的代码如下:

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

谢谢rondogiannis.this代码有时会写错,但这非常好。 – cengiz

+0

@ user1925149不客气! –