Guest User

Untitled

a guest
Jun 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct tree {
  4. char letra;
  5. struct tree *izq;
  6. struct tree *der;
  7. };
  8. struct tree *inicio;
  9. void preorden(struct tree *z){
  10. /* raiz izquierda derecha*/
  11. printf("%c",z->letra);
  12. if (z->izq != NULL) preorden(z->izq);
  13. if (z->der != NULL) preorden(z->der);
  14. }
  15. void inorden(struct tree *z){
  16. /*izquierda raiz derecha*/
  17. if (z->izq != NULL) inorden(z->izq);
  18. printf("%c",z->letra);
  19. if (z->der != NULL) inorden(z->der);
  20. }
  21. void postorden(struct tree *z){
  22. /*izquierda derecha raiz*/
  23. if (z->izq != NULL) postorden(z->izq);
  24. if (z->der != NULL) postorden(z->der);
  25. printf("%c",z->letra);
  26. }
  27. void insertar (struct tree *z,char l){
  28. struct tree *aux = inicio;
  29. struct tree *ant;
  30. /*para empezar llenamo nuestra estructura con la letra entregada l*/
  31. z->letra = l;
  32. /*si el inicio esta en null significa que nuetro arbol esta vacio se coloca el puntero inicio en nuetra estructura*/
  33. if (inicio == NULL){
  34. inicio = z;
  35. z->izq=NULL;
  36. z->der=NULL;
  37. }else{
  38. /*si esta lleno buscamo la hoja de la cual colgara nuetra nueva estructura*/
  39. do{
  40. ant = aux;
  41. /*el aux empieza en inicio, si nuestra letra es mayor se va este a la izquierda si no a la derecha */
  42. if (l > aux->letra) aux = aux->izq;else aux = aux->der;
  43. }while(aux!=NULL);
  44. /*hasta que aux sea nulo entonces la hoja de la cual enlazaremos nuestra estructura sera ant*/
  45. if (l > ant->letra)ant->izq = z;else ant->der=z;
  46. /*si l es mayor se enlaza por la izq si es menor por la derecha*/
  47. z->izq = NULL;
  48. z->der = NULL;
  49. /*de nuestra hoja sus nodos a null*/
  50. }
  51. }
  52. void main(){
  53. int i = 0;
  54. char pal[20];
  55. struct tree *p;
  56. inicio = NULL;
  57. printf("\n\n\tingrese palabra > ");
  58. scanf("%s",&pal);
  59. /*en este do while se llena nuestro arbol se crea una estructura y se le pasa a insertar hunto con una letra de nuestra plabra esto se repeite hasta que se encuentra el caracter nulo*/
  60. do{
  61. p=(struct tree *) malloc (sizeof(struct tree));
  62. insertar(p,pal[i]);
  63. i++;
  64. }while( pal[i]!= '\0' );
  65. printf("\n\t\tpreorden > ");preorden(inicio);
  66. printf("\n\t\tinorden > ");inorden(inicio);
  67. printf("\n\t\tpostorden > ");postorden(inicio);
  68. getchar();
  69. printf("\n\n");
  70. }
Add Comment
Please, Sign In to add comment