Im blocat cu umplere Array meu BST Bazat în C ++

voturi
0

Im încercarea de a construi o matrice pe bază de „binar de căutare copac“, urmând algoritmul de la:

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

... folosind algoritmul am venit cu următorul cod:

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

  }

Întrebări:

Nu pot să dau seama ce p [z] <- y înseamnă .... Im doar incrementarea rădăcină de a imita o traversare.

Dacă am deja un copil stânga / dreapta, atunci eu ar trebui să facă ca stânga / dreapta copil taht im suprascrie rădăcină? În aceasta i ar trebui să facă recurisve astfel că va trece înapoi la rădăcina originală, „R“?

Inserțiile introduce ( R); se introduce ( A); se introduce ( F); inserați ( L); se introduce ( B); se introduce ( C); inserați ( T);

Întrebat 14/11/2009 la 05:00
sursa de către utilizator
În alte limbi...                            


1 răspunsuri

voturi
1

Parerea mea este că, dacă / declarația altceva nu compară în mod corect:

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

de ce nu

(*aData) < items[root_index].theData

??

Metoda getName ar trebui să se întoarcă în esență, o copie a obiectului pentru comparație dvs. de a lucra.

Aici este metoda Inserare am scris-o pentru 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;
            }
        }

    };
Publicat 14/11/2009 la 05:22
sursa de către utilizator

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