Insert 2 elemenet trees ordered C

0

I am trying to insert 2 items (phone number and name), and when entering the phone number I find the name associated with that phone number. What I have currently only inserts the phone number and I do not know how to add the name and what they are associated with. (the code according to what I have works) Full code URL

Main code:

... switch (opcion)
    {
    case 1:
        printf("\nTelefono a insertar: ");
        scanf("%d",&aux);
        a = insertar(a, aux);
        printf("\nTelefono insertado");
       /* printf("\nNombre a insertar: ");
        scanf("%d",&aux2);
        a = insertar(a, aux2);
        printf("\nNombre insertado");
        */break;

    case 2:
        printf("\nNumero a encontrar: ");
        scanf("%d",&aux);
        if(encontrar(a, aux) != NULL)
        {
            printf("\nNumero de telefono: %d", encontrar(a, aux)->valor);
            //printf("\nNombre insertado: %d", encontrar(a, aux2)->nombre);
        }
        else
        {
            printf("\nNo encontrado");
        }
        break;


    }...

Code in the header:

#

ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>

typedef struct nodo_arbol
{
    int valor;                       /* Campo donde almacenaremos el valor */
    char nombre[255];
    struct nodo_arbol *izq;    /* Puntero al hijo izquierdo */
    struct nodo_arbol *der;      /* Puntero al hijo derecho */
}NODO_ARBOL, *P_NODO_ARBOL;


P_NODO_ARBOL crearArbol();
int esVacio(P_NODO_ARBOL a);
P_NODO_ARBOL insertar(P_NODO_ARBOL arbol, int i);
P_NODO_ARBOL encontrar (P_NODO_ARBOL arbol, int i);
void preOrder(P_NODO_ARBOL a);
void postOrder(P_NODO_ARBOL a);
void inOrder(P_NODO_ARBOL a);


#endif // HEADER_H_INCLUDED

Function.h:

P_NODO_ARBOL insertar(P_NODO_ARBOL arbol, int i)

{
    int x;
    P_NODO_ARBOL p;    /* Para no perder la cabeza del arbol */
    //P_NODO_ARBOL h;    /* Para no perder la cabeza del arbol */

    if (arbol == NULL)
    {
        p = alojar_nodo_arbol();             
        p->izq = p->der = NULL;             
        p->valor = i;

        return (p);
    }

    if (arbol->valor == i)    

        return (arbol);

    if (arbol->valor > i) /* "i" es menor que el valor que analizo, por tanto,
                           inserto a la izquierda */
        arbol->izq = insertar(arbol->izq, i);


    else  
        arbol->der = insertar(arbol->der, i);


    return (arbol);
}
    
asked by Trackless 08.06.2018 в 15:57
source

1 answer

0

I recommend the following. You want to store a name and a phone under the same variable name, this is easily solved by declaring a structure whose fields are the name and the phone number. If you apply this only insert nodes of this type of structure. PS: I recommend you manipulate the phones as character strings

typedef struct {
 char nombre[255];
 char telefono[16];
}datos;

typedef  struct nodo_arbol{
    datos info;
    struct nodo_arbol *izq;   
    struct nodo_arbol *der;    
}NODO_ARBOL, *P_NODO_ARBOL;

Then when you insert you decide which field to order, say by phone or by name. The comparison of whether the current chain is smaller than the one to be inserted can be found with strcmp or some variant.

The way to access each field would be NODO_ARBOL p; p-> info.name;

I hope it works for you, any questions tell me.

    
answered by 30.07.2018 в 05:45