Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct wezel
  5. {
  6. struct wezel *p;
  7. int k;
  8. struct wezel *left;
  9. struct wezel *right;
  10. };
  11.  
  12. void bst_wyswietl(struct wezel *x);
  13. struct wezel *bst_dodaj(struct wezel *root, struct wezel *nowy);
  14. struct wezel *bst_szukaj(struct wezel *root, int liczba);
  15. struct wezel *bst_usun(struct wezel *root, struct wezel* u);
  16. struct wezel *bst_min(struct wezel *root);
  17. struct wezel *bst_nast(struct wezel *x);
  18. struct wezel* bst_odwroc(struct wezel *root);
  19. struct wezel * bst_zwolnij(struct wezel *root);
  20.  
  21. int main()
  22. {
  23. struct wezel *root=NULL, *nowy=NULL, *x=NULL;
  24. char z;
  25. int liczba;
  26. while(1)
  27. {
  28. printf("Co chcesz zrobic?");
  29. printf("\nd - dodac");
  30. printf("\ns - szukac");
  31. printf("\nu - usunac");
  32. printf("\nm - minimum");
  33. printf("\nn - nastepnik");
  34. printf("\no - odwrocic liste");
  35. printf("\nw - wyswietlic");
  36. printf("\nq - wyjsc\n");
  37. fflush(stdin);
  38. z=getchar();
  39. switch(z)
  40. {
  41. case 'd':
  42. nowy=(struct wezel*)malloc(sizeof(struct wezel));
  43. printf("\nPodaj wartosc wezla do wstawienia: ");
  44. scanf("%d",&liczba);
  45. nowy->k=liczba;
  46. nowy->left=NULL;
  47. nowy->right=NULL;
  48. root=bst_dodaj(root,nowy);
  49. break;
  50. case 's':printf("Podaj wartosc elementu do wyszukania");
  51. scanf("%d",&liczba);
  52. nowy=bst_szukaj(root,liczba);
  53. printf("Wartosc wskaźnika wynosi %d",nowy);
  54. break;
  55. case 'u':
  56. break;
  57. case 'm':nowy=bst_min(root);
  58. printf("%d",nowy->k);
  59. break;
  60. case 'n':printf("Podaj wartosc wezla");
  61. scanf("%d",&liczba);
  62. nowy=bst_szukaj(root,liczba);
  63. nowy=bst_nast(nowy);
  64. printf("%d",nowy->k);
  65. break;
  66. case 'o':
  67. break;
  68. case 'w':bst_wyswietl(root);
  69. break;
  70. case 'q':
  71. return 0;
  72. default :
  73. printf("Podano zly znak. Podaj poprawny.");
  74. break;
  75. }
  76. }
  77. }
  78.  
  79. void bst_wyswietl(struct wezel *x)
  80. {
  81. if(x!=NULL)
  82. {
  83.  
  84. bst_wyswietl(x->left);
  85. printf("%d ",x->k);
  86. bst_wyswietl(x->right);
  87. }
  88. }
  89.  
  90.  
  91. struct wezel *bst_dodaj(struct wezel *root, struct wezel *nowy)
  92. {
  93. struct wezel *y=NULL;
  94. struct wezel *x=root;
  95. while(x!=NULL)
  96. {
  97. y=x;
  98. if(nowy->k<x->k)
  99. x=x->left;
  100. else
  101. x=x->right;
  102. }
  103. nowy->p=y;
  104. if(y==NULL)
  105. root=nowy;
  106. else
  107. {
  108. if(nowy->k<y->k)
  109. y->left=nowy;
  110. else
  111. y->right=nowy;
  112. }
  113. return root;
  114. }
  115. struct wezel *bst_szukaj(struct wezel *x, int liczba)
  116. {
  117. if(x==NULL || liczba==x->k)
  118. return x;
  119. if(liczba < x->k)
  120. return bst_szukaj(x->left,liczba);
  121. else
  122. return bst_szukaj(x->right,liczba);
  123. }
  124. struct wezel *bst_usun(struct wezel *root, struct wezel* u)
  125. {
  126. struct wezel *x=root;
  127. while(x!=NULL)
  128. {
  129.  
  130. }
  131. }
  132. struct wezel *bst_min(struct wezel *root)
  133. {
  134. struct wezel *x=root;
  135. while(x->left!=NULL)
  136. x=x->left;
  137. return x;
  138. }
  139. struct wezel *bst_nast(struct wezel *x)
  140. {
  141. struct wezel *y=NULL;
  142. if(x->right!=NULL)
  143. return bst_min(x->right);
  144. y=x->p;
  145. while(x!=NULL && x==y->right)
  146. {
  147. x=y;
  148. y=y->p;
  149. }
  150. return y;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement