2012-12-23 92 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不客氣! –