मेरे पेड़ कार्यक्रम एक रूट नोड में डालने के बाद दुर्घटनाओं

वोट
0

मैं पेड़ों बनाने में बहुत अच्छा नहीं हूँ और मैं पूरी तरह से प्रत्यावर्तन अप पेंच। हालांकि, मैं डाल सकते हैं और पेड़ में डेटा प्रदर्शित करने के लिए एक कार्यक्रम बनाने का प्रयास किया।

समस्या यह है कि यह रूट नोड में डालने के बाद दुर्घटनाओं और मैं पता नहीं क्यों है। पेड़ बहुत बड़ा नहीं है। सिर्फ 10 int

#include <stdio.h>
#include <stdlib.h>
#define SIZE 10;
/* run this program using the console pauser or add your own getch, system(pause) or input loop */
struct node{
    int data;
    struct node * left;
    struct node * right;
};


void insert(struct node * root,int num){
    printf(Insert called for num:%d\n,num);
    if(root == NULL){
        root = (struct node *)malloc(sizeof(struct node));
        root->data = num;
    }else if(num > root->data){ // Number greater than root ?
        insert(root->right,num); // Let the right sub-tree deal with it
    }else if(num < root->data){// Number less than root ?
        insert(root->left,num);// Let the left sub-tree deal with it.
    }else{
        // nothing, just return.
    }
}


void display(struct node * root){ // Inorder traversal
    if(root->left!=NULL){ // We still have children  in left sub-tree ?
        display(root->left); // Display them.
    }

    printf(%d,root->data); // Display the root data

    if(root->right!=NULL){ // We still have children in right sub-tree ?
        display(root->right); // Display them.
    }

}

int main(int argc, char *argv[]) {
    int a[10] = {2,1,3,5,4,6,7,9,8,10};
    int i;
    struct node * tree;

    for(i = 0; i < 10;i++){
        insert(tree,a[i]);
    }
    printf(Insert done);
    return 0;
}  

मैं कहाँ गलत हो गया था कोई मुझे बता सकते हैं?

मैं जानता हूँ कि यह ढेर पर अपने कोड की समीक्षा करने के लेकिन कभी कभी लोगों को पूछने के लिए पर सिकोड़ी है प्रोग्रामिंग जोड़ी काम करता है: p

अपडेट करें:
स्थापित करने के बाद struct node * tree = NULL;, insert()विधि अच्छी तरह से काम करता है। display()कारणों कार्यक्रम दुर्घटना।

02/10/2013 को 05:54
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


1 जवाब

वोट
2

अपनी

int main(int argc, char *argv[]) {
    // ...
    struct node * tree;
    // what is the value of tree at this line?
    for(i = 0; i < 10;i++){
        insert(tree,a[i]);
    }
    // ...
} 

क्या "पेड़" लाइन पर इंगित चिह्नित करता है?

02/10/2013 को 05:57
का स्रोत उपयोगकर्ता

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