कैसे एक सरणी BST काम के लिए एक डालने तरह?

वोट
1

मैं इसे recursively..The माता पिता पूर्णांक varaible होगा मैं की तरह है करने के लिए, सूत्र के अनुरूप, की कोशिश की है 2*i +1के लिए leftChildकी और 2*i +2सही के लिए।

void BST::insert(const data &aData)
{
    if ( items[Parent].empty ) 
    {
        items[Parent].theData = aData;
        items[Parent].empty = false;
    }
    else if ( aData < items[Parent].theData )
    {
        Parent = 2 * Parent + 1;
        if ( Parent >= maxSize ) this->reallocate();
        this->insert(aData);
    }
    else
    {
        Parent = (2 * rightChild++)+ 2;
        if ( Parent >= maxSize ) this->reallocate();
        this->insert(aData);
    }
}

यह ठीक काम करता है जब आइटम है कि मूल माता पिता की तुलना में कम कर रहे हैं डालने ... लेकिन जब मैं कुछ पाते हैं कि इसे बड़े सभी शिकंजा है: एक्स

void BST::reallocate()
{
    item *new_array = new item[maxSize*2];

    for ( int array_index = 0; array_index < maxSize; array_index++ ) 
    {
        if ( ! items[array_index].empty )
        {
            new_array[array_index].theData = items[array_index].theData;
        }
    }
    maxSize *= 2;
    delete [] items;

    items = NULL;
    items = new_array;
}

यहाँ मेरी ctor है ताकि कोई भी अब और भ्रमित हो जाता है तो मैं हूँ:

BST::BST(int capacity) : items(new item[capacity]), size(0), Parent(0),
leftChild(0), rightChild(0)
{
    items->empty = true;
    maxSize = capacity;
}
private:
    int size;  // size of the ever growing/expanding tree :)
    int Parent;
    int maxSize;    
    int leftChild;
    int rightChild;
    struct item
    {
        bool empty;
        data theData;
    };
    item *items;    // The tree array

ऊपर प्रविष्टि समारोह वास्तव में सबसे अच्छा मैं प्राप्त कर सकते हैं ..

                                 R
                                / \
                               /   \
                              /     \
                             L       X
                            / \     / \
                           J   V   K   T   <--The only out of place node.
                          / \   \
                         / NULL  \
                        G        /
                                P

जब डालने: R, L, J, G, X, K, V, P, Tइसी क्रम में

18/11/2009 को 01:44
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


1 जवाब

वोट
1

मुझे लगता है अपनी समस्या इस लाइन पर है:

    Parent = (2 * rightChild++)+ 2;

क्यों आप के बजाय यहां rightChild उपयोग कर रहे हैं (2 * Parent) + 2?

बातें स्पष्ट करने के लिए, आप बाईं / सही बच्चों और माता पिता की अनुक्रमणिका कंप्यूटिंग, एक सूचकांक दिया के लिए अपने वर्ग के लिए कुछ सरल इनलाइन कार्यों को जोड़ने में कर सकते हैं:

inline int getLeftChildIndex(int nodeNdx) { return (nodeNdx * 2) + 1; }
inline int getRightChildIndex(int nodeNdx) { ... }
inline int getParentIndex(int nodeNdx) { ... }

तुम भी वर्गों का उपयोग पर विचार कर सकते search()या find()विधि (मैं इसे एक है संभालने हूँ) है, जहां एक नए नोड डालने के लिए निर्धारित करने के लिए। खोज समारोह या तो एक मौजूदा नोड के सूचकांक लौटना चाहिए या जहां नए मूल्य डाला जाना चाहिए के सूचकांक (यह कैसे डुप्लिकेट मानों की प्रविष्टि को संभालने के लिए तय करने के लिए आप पर निर्भर है)।

24/11/2009 को 01:08
का स्रोत उपयोगकर्ता

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