Im geplakt met het vullen van mijn array gebaseerde BST in C ++

stemmen
0

Im proberen om een ​​array gebaseerde, Binary Search Tree op te bouwen door het volgen van het algoritme op:

http://highered.mcgraw-hill.com/olcweb/cgi/pluginpop.cgi?it=gif::600::388::/sites/dl/free/0070131511/25327/tree_insert.gif::TREE-INSERT NEGEREN .

... met behulp van het algoritme ik kwam met de volgende code:

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;
    }

  }

vragen:

Ik kan niet achterhalen wat p [z] <- y middel .... Im enkel verhogen van de wortel naar een traverse imiteren.

Als ik al een links / rechts kind dan moet ik dat links / rechts kind maken taht im over naar de root overschrijven? Daarin ik moet het recurisve dus het zal terug naar de oorspronkelijke wortel, R?

Inserties voegen ( R); Plaats ( A); Plaats ( F); Plaats ( L); Plaats ( B); Plaats ( C); Plaats ( T);

De vraag is gesteld op 14/11/2009 om 05:00
bron van user
In andere talen...                            


1 antwoorden

stemmen
1

ik denk dat je if / else statement niet goed is te vergelijken:

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

waarom niet doen

(*aData) < items[root_index].theData

??

De getName methode zou moeten wezen een kopie van het object terug te keren voor uw vergelijking te werken.

Hier is de methode Insert ik schreef voor 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;
            }
        }

    };
antwoordde op 14/11/2009 om 05:22
bron van user

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