Im застрял с заполнением моего массива из BST в C ++

голоса
0

Im пытается создать массив на основе, «Binary Search Tree», следуя алгоритму по адресу:

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 означает .... я просто приращение корня подражать траверс.

Если у меня уже есть левый / правый ребенок, то я должен сделать это левый / правый ребенок TAHT им о том, чтобы заменить корень? В этом я должен сделать это recurisve так он переключится обратно на исходный корень, «R»?

Вставки вставки ( R); Вставка ( А); Вставка ( F); Вставка ( L); вставки ( B); вставки ( C); Вставка ( Т);

Задан 14/11/2009 в 05:00
источник пользователем
На других языках...                            


1 ответов

голоса
1

я думаю, что ваше если / другое заявление не сравнивает правильно:

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

почему бы не сделать

(*aData) < items[root_index].theData

??

Способ GetName бы по существу вернуть копию объекта для сравнения, чтобы работать.

Вот метод Insert я написал для 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