BST कार्यान्वयन

वोट
1

क्या एक के निम्नलिखित कार्यान्वयन के साथ गलत क्या है द्विआधारी खोज वृक्ष (बीएसटी) ? मुझे बताया गया है कि यह करने के लिए एक सूचक सूचक का उपयोग करने के लिए बेहतर है structडालने समारोह में एक तर्क के रूप नोड।

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

insert(int key, struct node *leaf)
{
    if( leaf == 0 )
    {
        leaf = (struct node*) malloc( sizeof( struct node ) );
        leaf->key_value = key;
        /* initialize the children to null */
        leaf->left = 0;    
        leaf->right = 0;  
    }
    else if(key < leaf->key_value)
    {
        insert( key, leaf->left );
    }
    else if(key > leaf->key_value)
    {
        insert( key, leaf->right );
    }
}
13/01/2010 को 11:01
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


3 जवाब

वोट
1

एक नोड डाला जाता है जब (पत्ता == 0), आप अपनी मूल परिवर्तन नहीं किया है, इसलिए नए नोड एक अनाथ हो जाएगा।

दूसरे शब्दों में, अपने पेड़ अभी भी केवल एक नोड की तरह दिखाई देगा, कोई फर्क नहीं पड़ता कि कितने नोड्स डालने के साथ कहा जाता था।

13/01/2010 को 12:32
का स्रोत उपयोगकर्ता

वोट
2

यह रेखा:

leaf = (struct node*) malloc( sizeof( struct node ) );

के लिए एक नया मूल्य देता है leaf, कुछ नव आबंटित स्मृति में यह इशारा करते हुए। हालांकि, नए मूल्य समारोह नहीं छोड़ता। समारोह लौटाता है, तो फोन करने वाले अभी भी पुराने की चर्चा करते हुए किया जाएगा leaf, और एक स्मृति रिसाव नहीं होगा।

दो दृष्टिकोण आप इसे तय करने के लिए अपना सकते हैं:

1. एक सूचक है, जैसे करने के लिए एक सूचक का प्रयोग करें

void insert(int key, struct node **leaf)
{
    if(*leaf == 0 )
    {
        *leaf = (struct node*) malloc( sizeof( struct node ) );
        ...
}

/* In caller -- & is prepended to current_leaf. */
insert(37, &current_leaf);

2. नई पत्ती लाभ (या पुराने पत्ती अगर कोई परिवर्तन होता है)।

struct node *insert(int key, struct node *leaf)
{
    if(leaf == 0 )
    {
        leaf = (struct node*) malloc( sizeof( struct node ) );
        ...
    }

    return leaf;
}

/* In caller -- & is prepended to current_leaf. */
current_leaf = insert(37, current_leaf);

संकेत की ओर इशारा कठिन जा रहा है समझने के लिए की दहलीज के करीब हैं। मैं शायद, दूसरा विकल्प के लिए जाना है, तो होगा insertवर्तमान में कुछ और लौटा रहा है।

13/01/2010 को 12:48
का स्रोत उपयोगकर्ता

वोट
-2
  #include<stdio.h>
     typedef struct tnode{
       int data;
       struct tnode *left,*right;
     }TNODE;
     TNODE * createTNode(int key){
       TNODE *nnode;
       nnode=(TNODE *)malloc(sizeof(TNODE));    
       nnode->data=key;
       nnode->left=NULL;
       nnode->right=NULL;
      return nnode;
}

    TNODE * insertBST(TNODE *root,int key){
     TNODE *nnode,*parent,*temp;
     temp=root;
      while(temp){
        parent=temp;
        if(temp->data > key)
            temp=temp->left;
        else
            temp=temp->right;    
    }    
     nnode=createTNode(key);
    if(root==NULL)
        root=nnode;
    else if(parent->data>key)
        parent->left=nnode;
    else
        parent->right=nnode;
    return root;
}

     void preorder(TNODE *root){
       if(root){
         printf("%5d",root->data);    
         preorder(root->left);
         preorder(root->right);
       }    
     }  

    void inorder(TNODE *root){
       if(root){
        inorder(root->left);
        printf("%5d",root->data);    
        inorder(root->right);
      }    
    }

    void postorder(TNODE *root){
       if(root){
        postorder(root->left);    
        postorder(root->right);
        printf("%5d",root->data);
      }    
    }

     main(){
       TNODE *root=NULL;
       int ch,key;
       do{
         printf("\n\n1-Insert\t2-Preorder\n3-Inorder\t4-Postorder\n5-Exit\n");
         printf("Enter Your Choice: ");
         scanf("%d",&ch);  

        switch(ch){ 
            case 1:
                printf("Enter Element: ");
                scanf("%d",&key);
                root=insertBST(root,key);
                break;
            case 2:
                preorder(root);
                break;
            case 3:
                inorder(root);
                break;
            case 4:
                postorder(root);
                break;
            default:
                printf("\nWrong Choice!!");
        }

    }while(ch!=5);
    getch();
    return 0;
}
04/10/2013 को 14:31
का स्रोत उपयोगकर्ता

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