द्विआधारी खोज वृक्ष सी ++ (माता पिता)

वोट
0

मैं अपने BST पर सिर्फ एक छोटे से अधिक मदद की जरूरत है। यह मेरा BST सम्मिलित करते समय कैसा दिखता है:

आर, एल, जे, जी

                      R   --Root at Index 0
                     / \
     L @ Index1     L   NULL
                   / \
     J @ Index3   J   NULL
                 / \
     G @ Index7 G  NULL

यहाँ कोड ऐसा बनाता है।

void BST::insert(const data &aData)
{   
    if ( items[Parent].empty ) 
    {
        items[Parent].theData = aData; // insert at leaf.
        items[Parent].empty = false;
        size++;

        return;
    }           
    for ( int i = 0; i <= size; i++ )
    {
        if ( aData < items[Parent].theData )
        {
            if ( items[2*i+1].empty )
            {
            items[2*i+1].theData = aData;
            items[2*i+1].empty = false;
            }
            else 
                           {
            // we must already have a left child to some root.
                                Parent++;  So make the previous data the root???
            if ( items[Parent].empty )
            {
                items[Parent].theData = items[2*i+1].theData;
                items[Parent].empty   = false;
                Parent = (i-1)/2;
            }
                           } 
        }
        else
        { ...// do the same for data greater than but with items[2*i+2] }

मेरा प्रश्न है कि एक नया रूट बनाने के लिए जब मैं की आवश्यकता होगी है? जब मैं एक नया रूट बनाने के लिए की आवश्यकता होगी? recomparison के लिए?

इस दृष्टिकोण सही है? जो लोग भी दोनों मेरी पोस्ट को देखने के लिए करने के लिए धन्यवाद :)

// निर्माता BST क्लास और अपने निजी अनुभाग।

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
17/11/2009 को 21:59
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


2 जवाब

वोट
1

आपका तर्क (बल्कि फजी मुझे कहना पड़ेगा) गलत प्रतीत होता है: क्या "अगर" की तरह अनुक्रम है?

if ( items[2*i+1].empty )
{
}
else if (!items[2*i+1].empty)
{
   if ( items[2*i+1].empty )
   {
        // any code here is unreachable
   }
}
17/11/2009 को 22:12
का स्रोत उपयोगकर्ता

वोट
1

मेरा सुझाव है कि आप इस reimplement रिकर्सिवली काम करने के लिए। कुछ इस तरह:

void BST::insert(const data& aData, int pos) {
    if (items[pos].empty) {
        // insert it here
    }
    else (aData < items[pos].theData) {
        // go to left child
        insert(aData, 2*pos + 1);
    }
    else {
        // go to right child
        insert(aData, 2*pos + 2);
    }
}

यह वास्तव में स्पष्ट नहीं है कि क्या माता पिता, leftChild, और rightChild अपनी कक्षा में कर रहे हैं, लेकिन यह एक अलग मुद्दा है।

17/11/2009 को 22:46
का स्रोत उपयोगकर्ता

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