İkili Arama ağacı sorunu

oy
-2

Bu programda segment hataya karşı karşıyayım. Olmasını anladım olarak akış doğru gibi görünüyor. Bana bu programda hata öğrenmek yardımcı olun.

#include<iostream>
#include<cstdlib>

using namespace std; 

struct node 
{ 
    int data; 
    struct node* left; 
    struct node* right; 
}; 

typedef struct node* Node; 
void insert(Node,int); 
Node root = NULL; 
int main() 
{ 
    insert(root,2); 
    insert(root,1); 
    insert(root,3); 

    cout<<root->data<< <<root->left->data<< <<root->right->data<<endl; 
    return 0; 
} 

void insert(Node nod,int val) 
{ 
    if(nod == NULL) 
    {
        Node newnode = new(struct node); 
        newnode->data = val; 
        newnode->left = NULL; 
        newnode->right = NULL; 
        nod = newnode; 
        if(root == NULL) 
        { 
            root = newnode; 
        } 
    } 
    else if(nod->data > val) 
    { 
        insert(node->left,val); 
    } 
    else if(nod->data < val) 
    {  
        insert(nod->right,val); 
    } 
}
Oluştur 04/09/2011 saat 11:23
kaynak kullanıcı
Diğer dillerde...                            


2 cevaplar

oy
5

Aslında setleri şey yok root->leftya root->right. Aramalar için insert(node->left, val)bunu yapacak bildiğini yapmak değildir. Aslında sol ve sağ işaretçileri değiştirmek için, işaretçiler adresi eklemek için geçmesi gerekiyor. yani insert(&node->left, val), ve değişime insertidare.

Cevap 04/09/2011 saat 11:40
kaynak kullanıcı

oy
0

basit. ekinizi değiştirin:

void insert(Node &nod,int val) 
{ 
  if(nod == NULL) {

    Node newnode = new(struct node); 
    newnode->data = val; 
    newnode->left = NULL; 
    newnode->right = NULL; 
    nod = newnode; 
  } 
  else if(nod->data > val) 
    { 
      insert(nod->left,val); 
    } 
  else if(nod->data < val) 
    {  
      insert(nod->right,val); 
    } 
}
Cevap 04/09/2011 saat 11:44
kaynak kullanıcı

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more