Im stecken mit meinem Array Based BST in C ++ Füllung

stimmen
0

Im Versuch, ein Array basiert zu bauen, „Binary Search Tree“ von nach dem Algorithmus an:

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

... mit dem Algorithmus I mit dem folgenden Code kam:

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

  }

Fragen:

Ich kann nicht herausfinden, was p [z] <- y Mittel .... Im nur die Wurzel des Zunehmen eine Traverse zu imitieren.

Wenn ich schon ein linkes / rechtes Kind haben, dann sollte ich das linke / rechte Kind machen taht im über die Wurzel zu überschreiben? Darin sollte ich es machen recurisve so wird es auf die ursprüngliche Wurzel zurückzuschalten, „R“?

Einfügungen einzufügen ( R); Einfügen ( A); insert ( F); Einfügen ( L); Einfügen ( B); Einfügen ( C); insert ( T);

Veröffentlicht am 14/11/2009 um 05:00
quelle vom benutzer
In anderen Sprachen...                            


1 antworten

stimmen
1

Meine Vermutung ist, dass Ihre if / else-Anweisung ist nicht richtig zu vergleichen:

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

warum nicht tun

(*aData) < items[root_index].theData

??

Die getName Methode müßte im Wesentlichen eine Kopie des Objekts für Ihren Vergleich zurückkehren zu arbeiten.

Hier ist die Insert-Methode I für BST hat folgendes geschrieben:

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

    };
Beantwortet am 14/11/2009 um 05:22
quelle vom benutzer

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