द्विआधारी खोज वृक्ष क्रियान्वयन।

वोट
0

मैं द्विआधारी खोज वृक्ष को लागू करने की कोशिश कर रहा था, लेकिन मुझे लगता है कि मैं अपने डालने समारोह में एक गलती की है। यहाँ मेरी कोड है

#include<iostream>
#include<memory.h>
#include <cstddef>
using namespace std;
struct bst_node
{
    int info;
    struct bst_node *left_node_ptr;
    struct bst_node *right_node_ptr;
};

struct bst_node* getnode(int x)
{

    struct bst_node* ret= new bst_node;
    ret->info=x;
    ret->left_node_ptr=NULL;
    ret->right_node_ptr=NULL;
    return ret;
}

void insert(struct bst_node **root, int var_info)
{
    struct bst_node *temp=(*root); // Links the temporary pointer to root of the BST
    while(temp!=NULL)              // Loop till I find a suitable position for inserting
    {
        if(temp->info > var_info)
        {
            temp=temp->left_node_ptr;
        }
        else
        {
            temp=temp->right_node_ptr;
        }

    }
    temp= getnode(var_info);
    return ;
}

/* Recursive In order Traversal */
void inorder_recursive( struct bst_node * L)
{
    if(L!= NULL)
    {
        inorder_recursive(L->left_node_ptr);
        cout<<L->info<<endl;
        inorder_recursive(L->right_node_ptr);
    }
    return;
}
int main()
{
    struct bst_node* my_root= getnode(5);
    insert(&my_root, 6);
    insert(&my_root, 3);
    /*
    int x=1;
    int arr[]= {};
    while(x)
    {
        cin>>x;
        insert(&my_root, x);
    }*/
    inorder_recursive(my_root);
    return 0;
}
31/03/2011 को 11:33
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


2 जवाब

वोट
2

आप वास्तव में सेट नहीं left_node_ptrया right_node_ptrअपने नोड्स के मूल्यों। आपका डालने समारोह पेड़ नए नोड डाल करने के लिए सही जगह खोजने नीचे चलाता है, तो नोड आवंटित करता है - लेकिन वास्तव में बाएं या माता-पिता आप पाया के अधिकार के लिए नए नोड संलग्न नहीं है।

31/03/2011 को 11:39
का स्रोत उपयोगकर्ता

वोट
1

अपनी खोज को बहुत दूर 1 स्तर चला जाता है। आप नोड फेंक दिया जो आप अपने नए बच्चे अनुलग्न करना चाहते हैं करने के लिए। इसके अलावा अस्थायी = ... अपने पेड़ के लिए कुछ भी संलग्न नहीं होंगे। आप थोड़ी देर कर जब तक आप चाइल्ड नोड आप के लिए देते हैं और उसके बाद करना या तो करना चाहते हैं चाहिए:

temp-> left_node_ptr = getnode (var_info); या temp-> right_node_ptr = getnode (var_info);

   while(temp!=NULL)              // Loop till I find a suitable position for inserting
        {
            if(temp->info > var_info)
            {
                temp=temp->left_node_ptr;
            }
            else
            {
                temp=temp->right_node_ptr;
            }

        }
        temp= getnode(var_info);
31/03/2011 को 11:39
का स्रोत उपयोगकर्ता

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