Estoy atascado con el llenado de mi BST basado en Array en C ++

votos
0

Estoy tratando de construir una matriz basada en Árbol de búsqueda binaria siguiendo el algoritmo en:

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

... usando el algoritmo, se me ocurrió el siguiente código:

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

  }

Preguntas:

No puedo entender lo que p [z] <- y significa ... Solo estoy incrementando la raíz para imitar una poligonal.

Si ya tengo un hijo izquierdo / derecho, ¿debería hacer que ese hijo izquierdo / derecho esté a punto de sobrescribir la raíz? ¿Debo hacer que vuelva a aparecer para que vuelva a la raíz original, R?

Inserción de inserciones (R); insertar (A); insertar (F); insertar (L); insertar (B); insertar (C); insertar (T);

Publicado el 14/11/2009 a las 05:00
fuente por usuario
En otros idiomas...                            


1 respuestas

votos
1

mi suposición es que su declaración if / else no está comparando correctamente:

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

por qué no hacer

(*aData) < items[root_index].theData

??

El método getName tendría que devolver esencialmente una copia del objeto para que funcione su comparación.

Aquí está el método de inserción que escribí para 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;
            }
        }

    };
Respondida el 14/11/2009 a las 05:22
fuente por usuario

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