इम सी में मेरी सरणी के आधार BST भरने के साथ फंस ++

वोट
0

इम पर एल्गोरिथ्म का पालन करते हुए आधारित, बाइनरी खोजें ट्री एक सरणी बनाने की कोशिश:

http://highered.mcgraw-hill.com/olcweb/cgi/pluginpop.cgi?it=gif::600::388::/sites/dl/free/0070131511/25327/tree_insert.gif::TREE-INSERT ध्यान न दें

... एल्गोरिथ्म मैं निम्नलिखित कोड के साथ आया था का उपयोग कर:

void BST::insert( const data &aData )
{
     item *y = &items[root_index];   // Algorithm calls for NULL assignment..
     item *x = &items[root_index]; 

while ( ! items[root_index].empty )
{
    y->theData = x->theData; // Ptrs are required or else a crash occurs.
    if ( aData < x->theData )
    {
        x->leftChild = aData;
    }
    else
    {
        x->rightChild = items[root_index].theData;
    } 

    // what is p[z] = y? is it outside the looping scheme?

    root_index++; // and make the new child the root?   
}
    if ( y->empty ) 
    {
        items[root_index].theData = aData;
        items[root_index].empty = false;
    }
    else if ( aData < y->theData )
    {
        y->leftChild = aData; 
    // If we already have a left/right child...make it the root? re-cmpr?
              }
    else
    {
        y->rightChild = items[root_index].theData;
    }

  }

प्रशन:

y साधन .... मैं सिर्फ एक पार की नकल करने की जड़ incrementing - मैं क्या p [z] <को समझ नहीं सकता।

मैं पहले से ही एक छोड़ दिया / सही बच्चा है तो मुझे लगता है कि बायीं / दायीं बच्चे बनाना चाहिए taht im के बारे में जड़ अधिलेखित करने के लिए? उसमें मैं यह इतना recurisve यह मूल जड़, आर वापस स्विच करेंगे करना चाहिए?

निवेशन सम्मिलित ( आर); सम्मिलित ( 'ए'); सम्मिलित ( 'एफ'); सम्मिलित ( एल); सम्मिलित ( बी); सम्मिलित ( 'सी'); सम्मिलित ( टी);

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


1 जवाब

वोट
1

मेरा अनुमान है कि आपके अगर / किसी और बयान को सही ढंग से की तुलना नहीं है:

aData->getName() < items[root_index].theData

क्यों नहीं

(*aData) < items[root_index].theData

??

getName विधि अनिवार्य रूप से काम करने के लिए अपनी तुलना के लिए वस्तु की एक प्रति वापस जाने के लिए होगा।

यहाँ सम्मिलित विधि मैं BST के लिए लिखा है:

    /* Insert by node */
    template<class T>
    void Tree<T>::Insert(Node<T> *insertingNode)
    {
        Node<T> *y = NULL;
        Node<T> *x = &this->Root;

        while( x != NULL)
        {
            // y is used as a temp
            y = x;

            // given value less than current
            if(insertingNode->value < x->value)
            {
                // move to left of current
                x = x->ptrLeft;
            }
            else
            {
                // move to right of current
                x = x->ptrRight;
            }
        }

        // now, we're in place
        // parent of inserting value is last node of loop
        insertingNode->ptrParent = y;

        // if there is no node here, insert the root
        if (y == NULL)
        {
            Root = *insertingNode;
        }
        else
        {
            // Place inserting value accordingly
            if(insertingNode->value < y->value)
            {
                // Goes on the left
                y->ptrLeft = insertingNode;
            }
            else
            {
                // Goes on the right
                y->ptrRight = insertingNode;
            }
        }

    };
14/11/2009 को 05:22
का स्रोत उपयोगकर्ता

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