Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. struct element_drzewa
  6. {
  7. int klucz;
  8. char wart;
  9. struct element_drzewa *lewy_potomek, *prawy_potomek;
  10. }* lisc;
  11.  
  12. void dodaj_element(struct element_drzewa **lisc, int klucz, char wart)
  13. {
  14. while(*lisc!=NULL)
  15. {
  16. if((*lisc)->klucz >= klucz)
  17. lisc = &(*lisc)->lewy_potomek;
  18. else
  19. lisc = &(*lisc)->prawy_potomek;
  20. }
  21. *lisc = (struct element_drzewa *)malloc(sizeof(struct element_drzewa));
  22. if(*lisc)
  23. {
  24. (*lisc)->klucz = klucz;
  25. (*lisc)->wart = wart;
  26. (*lisc)->lewy_potomek = (*lisc)->prawy_potomek = NULL;
  27. }
  28. }
  29.  
  30. void postorder(struct element_drzewa *lisc)
  31. {
  32. if(lisc)
  33. {
  34. postorder(lisc->lewy_potomek);
  35. postorder(lisc->prawy_potomek);
  36. printf("klucz: %d , wartosc: %c\n", lisc->klucz, lisc->wart);
  37. }
  38. }
  39.  
  40. void preorder(struct element_drzewa *lisc)
  41. {
  42. if(lisc)
  43. {
  44. printf("klucz: %d , wartosc: %c\n", lisc->klucz, lisc->wart);
  45. preorder(lisc->lewy_potomek);
  46. preorder(lisc->prawy_potomek);
  47. }
  48. }
  49.  
  50. void inorder(struct element_drzewa *lisc)
  51. {
  52. if(lisc)
  53. {
  54. inorder(lisc->lewy_potomek);
  55. printf("klucz: %d , wartosc: %c\n", lisc->klucz, lisc->wart);
  56. inorder(lisc->prawy_potomek);
  57. }
  58. }
  59.  
  60. int main()
  61. {
  62. srand(time(NULL));
  63. int i;
  64. for(i=0;i<10;i++)
  65. {
  66. int klucz=0;
  67. char wart=0;
  68. wart = 'a'+rand()%('z'-'a'+1);
  69. klucz = (int) wart;
  70. dodaj_element(&lisc,klucz,wart);
  71. }
  72. puts("wyswietlanie preorder:");
  73. preorder(lisc);
  74. puts("wyswietlania postorder:");
  75. postorder(lisc);
  76. puts("wyswietlanie inorder:");
  77. inorder(lisc);
  78. return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement