aplicación BST

votos
1

Qué le pasa a la siguiente aplicación de un árbol de búsqueda binaria (BST) ? Me han dicho que es mejor usar el puntero a un puntero a structnodo como un argumento en función de inserción.

struct node
{
int key_value;
struct node* left;
struct node* right;
};

insert(int key, struct node *leaf)
{
    if( leaf == 0 )
    {
        leaf = (struct node*) malloc( sizeof( struct node ) );
        leaf->key_value = key;
        /* initialize the children to null */
        leaf->left = 0;    
        leaf->right = 0;  
    }
    else if(key < leaf->key_value)
    {
        insert( key, leaf->left );
    }
    else if(key > leaf->key_value)
    {
        insert( key, leaf->right );
    }
}
Publicado el 13/01/2010 a las 11:01
fuente por usuario
En otros idiomas...                            


3 respuestas

votos
1

Cuando un nodo se inserta (hoja == 0), que no cambió su matriz, por lo que el nuevo nodo se convertirá en un huérfano.

En otras palabras, el árbol sigue se verá como un solo nodo, no importa cuántos nodos se llama con parte.

Respondida el 13/01/2010 a las 12:32
fuente por usuario

votos
2

Esta línea:

leaf = (struct node*) malloc( sizeof( struct node ) );

da un nuevo valor a leaf, apuntando a algo de memoria recién asignada. Sin embargo, el nuevo valor no sale de la función. Cuando la función devuelve, la persona que llama todavía se refería a la antigua leaf, y habrá una pérdida de memoria.

Hay dos enfoques que se pueden tomar para su fijación:

1. Utilizar un puntero a un puntero, por ejemplo,

void insert(int key, struct node **leaf)
{
    if(*leaf == 0 )
    {
        *leaf = (struct node*) malloc( sizeof( struct node ) );
        ...
}

/* In caller -- & is prepended to current_leaf. */
insert(37, &current_leaf);

2. Vuelva a colocar la nueva hoja (o la edad de la hoja si no hay ningún cambio).

struct node *insert(int key, struct node *leaf)
{
    if(leaf == 0 )
    {
        leaf = (struct node*) malloc( sizeof( struct node ) );
        ...
    }

    return leaf;
}

/* In caller -- & is prepended to current_leaf. */
current_leaf = insert(37, current_leaf);

Los punteros a punteros están cerca del umbral de ser difícil de comprender. Yo probablemente vaya por la segunda opción, si insertno está actualmente devolver cualquier otra cosa.

Respondida el 13/01/2010 a las 12:48
fuente por usuario

votos
-2
  #include<stdio.h>
     typedef struct tnode{
       int data;
       struct tnode *left,*right;
     }TNODE;
     TNODE * createTNode(int key){
       TNODE *nnode;
       nnode=(TNODE *)malloc(sizeof(TNODE));    
       nnode->data=key;
       nnode->left=NULL;
       nnode->right=NULL;
      return nnode;
}

    TNODE * insertBST(TNODE *root,int key){
     TNODE *nnode,*parent,*temp;
     temp=root;
      while(temp){
        parent=temp;
        if(temp->data > key)
            temp=temp->left;
        else
            temp=temp->right;    
    }    
     nnode=createTNode(key);
    if(root==NULL)
        root=nnode;
    else if(parent->data>key)
        parent->left=nnode;
    else
        parent->right=nnode;
    return root;
}

     void preorder(TNODE *root){
       if(root){
         printf("%5d",root->data);    
         preorder(root->left);
         preorder(root->right);
       }    
     }  

    void inorder(TNODE *root){
       if(root){
        inorder(root->left);
        printf("%5d",root->data);    
        inorder(root->right);
      }    
    }

    void postorder(TNODE *root){
       if(root){
        postorder(root->left);    
        postorder(root->right);
        printf("%5d",root->data);
      }    
    }

     main(){
       TNODE *root=NULL;
       int ch,key;
       do{
         printf("\n\n1-Insert\t2-Preorder\n3-Inorder\t4-Postorder\n5-Exit\n");
         printf("Enter Your Choice: ");
         scanf("%d",&ch);  

        switch(ch){ 
            case 1:
                printf("Enter Element: ");
                scanf("%d",&key);
                root=insertBST(root,key);
                break;
            case 2:
                preorder(root);
                break;
            case 3:
                inorder(root);
                break;
            case 4:
                postorder(root);
                break;
            default:
                printf("\nWrong Choice!!");
        }

    }while(ch!=5);
    getch();
    return 0;
}
Respondida el 04/10/2013 a las 14:31
fuente por usuario

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