Sort alphabetically in a binary tree

1

It is a binary tree that has the purpose of keeping names in alphabetical order however I am not sure that I am passing the parameter correctly. I'm sorry if the code is a bit long.

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

struct nodo
{
  char nombre[10];
  struct nodo *izq,*der;
};

void inserta(struct nodo*insercion,struct nodo*elemento);
void imprime(struct nodo *p);
void borra(struct nodo *p);

int main()
{
  int i,n;
  char name[10];

  struct nodo *raiz,*temp;

  raiz=NULL;
  printf("Teclea nombres, seguido por un caracter no numerico\n");
  do{   
    gets(name);
    getchar();
    {
      temp=(struct nodo*)malloc(sizeof(struct nodo));
      temp->der=NULL;
      temp->izq=NULL;
      strcpy(temp->nombre,name);
      if(raiz==NULL)
      {
        raiz=temp;
      }
      else
      {
        inserta(raiz,temp);
      }
    }
  }

  printf("Listo\n");
  printf("Nombres impresos en orden\n");
  imprime(raiz);
  borra(raiz);
}

void inserta(struct nodo*insercion, struct nodo*elemento)
{
  struct nodo*avanza,*nuevo;

  avanza=insercion;

  if(avanza!=NULL)
  {
    if(strcmp(elemento->nombre,avanza->nombre)>0)
    {
      if(avanza->der!=NULL)
      {
        inserta(avanza->der,elemento);
      }
      else
      {
        avanza->der=elemento;
        return;
      }
    }
    if(strcmp(elemento->nombre,avanza->nombre)<=0)
    {
      if(avanza->izq!=NULL)
      {
        inserta(avanza->izq,elemento);
      }
      else
      {
        avanza->izq=elemento;
        return;
      }
    }
  }
}
    
asked by Master 11.02.2018 в 22:09
source

0 answers