Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. struct wezel
  4. {
  5. int wartosc;
  6. struct wezel *lewy;
  7. struct wezel *prawy;
  8. };
  9.  
  10.  
  11. struct wezel* newwezel(int wartosc)
  12. {
  13.  
  14. struct wezel* wezel = (struct wezel*)malloc(sizeof(struct wezel));
  15.  
  16.  
  17. wezel->wartosc = wartosc;
  18.  
  19.  
  20. wezel->lewy = NULL;
  21. wezel->prawy = NULL;
  22. return(wezel);
  23. }
  24. void printInorder(struct wezel* wezel)
  25. {
  26. if (wezel == NULL)
  27. return;
  28. printInorder(wezel->lewy);
  29. printf("%d ", wezel->wartosc);
  30. printInorder(wezel->prawy);
  31. }
  32. void printPreorder(struct wezel* wezel)
  33. {
  34. if (wezel == NULL)
  35. return;
  36. printf("%d ", wezel->wartosc);
  37. printPreorder(wezel->lewy);
  38. printPreorder(wezel->prawy);
  39. }
  40.  
  41. void printPostorder(struct wezel* wezel)
  42. {
  43. if (wezel == NULL)
  44. return;
  45. printPostorder(wezel->lewy);
  46. printPostorder(wezel->prawy);
  47. printf("%d ", wezel->wartosc);
  48. }
  49. struct wezel* wstawBST(struct wezel* wezel, int wartosc)
  50. {
  51.  
  52. if (wezel == NULL)
  53. return newwezel(wartosc);
  54.  
  55. if (wartosc < wezel->wartosc)
  56. wezel->lewy = wstawBST(wezel->lewy, wartosc);
  57. else if (wartosc > wezel->wartosc)
  58. wezel->prawy = wstawBST(wezel->prawy, wartosc);
  59.  
  60.  
  61. return wezel;
  62. }
  63. struct wezel* szukaj(struct wezel* korzen, int wartosc)
  64. {
  65.  
  66. if (korzen == NULL || korzen->wartosc == wartosc)
  67. return korzen;
  68.  
  69.  
  70. if (korzen->wartosc < wartosc)
  71. return szukaj(korzen->prawy, wartosc);
  72.  
  73. return szukaj(korzen->lewy, wartosc);
  74. }
  75.  
  76. int main()
  77. {
  78.  
  79. struct wezel *korzen = newwezel(79);
  80. wstawBST(korzen,13);
  81. wstawBST(korzen,23);
  82. wstawBST(korzen,15);
  83. wstawBST(korzen,111);
  84. wstawBST(korzen,64);
  85. wstawBST(korzen,34);
  86. printf("Post-order ");
  87. printPostorder(korzen);
  88. printf("\nPre-order ");
  89. printPreorder(korzen);
  90. printf("\nIn-order ");
  91. printInorder(korzen);
  92. printf("\n");
  93. return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement