GoralWMoro

BST

Jun 5th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct wezel{
  5.     struct wezel *ojciec;
  6.     struct wezel *lewy;
  7.     struct wezel *prawy;
  8.     int klucz;
  9. };
  10. char tab[100];
  11. int tab_iterator = 0;
  12. struct wezel *dodawanie(struct wezel *glowa,struct wezel *nowy){
  13.     struct wezel *pomoc=glowa;
  14.  
  15.     if(nowy->klucz < pomoc->klucz){
  16.         if(pomoc->lewy==NULL){
  17.             pomoc->lewy=nowy;
  18.             nowy->ojciec=pomoc;
  19.         }else dodawanie(pomoc->lewy, nowy);
  20.     }
  21.     if(nowy->klucz>pomoc->klucz){
  22.         if(pomoc->prawy==NULL){
  23.             pomoc->prawy=nowy;
  24.             nowy->ojciec=pomoc;
  25.         }else dodawanie(pomoc->prawy,nowy);
  26.     }
  27.  
  28.     return glowa;
  29. };
  30.  
  31.  
  32. void wypisywanie(struct wezel *glowa, int szukany){
  33.     struct wezel *pomoc = glowa;
  34.     if(glowa == NULL){
  35.         tab[0] = 'A';
  36.     }
  37.     if(pomoc->klucz == szukany){
  38.         tab[tab_iterator] = pomoc->klucz;
  39.         return;
  40.     }else if(pomoc->klucz > szukany && pomoc->lewy != NULL){
  41.         tab[tab_iterator] = pomoc->klucz;
  42.         tab_iterator++;
  43.         wypisywanie(pomoc->lewy, szukany);
  44.     }else if(pomoc->klucz < szukany && pomoc->prawy != NULL){
  45.         tab[tab_iterator] = pomoc->klucz;
  46.         tab_iterator++;
  47.         wypisywanie(pomoc->prawy, szukany);
  48.     }
  49. }
  50.  
  51. int main() {
  52.  
  53.     struct wezel *glowa=NULL;
  54.  
  55.     int dzialanie;
  56.     int wartosc;
  57.  
  58.     do{
  59.  
  60.         scanf("%d", &dzialanie);
  61.  
  62.         if (dzialanie == 0) {
  63.             return 0;
  64.         } else if (dzialanie == 1) {
  65.             struct wezel *nowy;
  66.  
  67.             nowy = (struct wezel *) malloc(sizeof(struct wezel));
  68.             scanf("%d", &wartosc);
  69.  
  70.             nowy->klucz = wartosc;
  71.             nowy->lewy = NULL;
  72.             nowy->prawy = NULL;
  73.             nowy->ojciec=NULL;
  74.  
  75.             if (glowa == NULL) {
  76.                 glowa = nowy;
  77.             }else {
  78.                 glowa = dodawanie(glowa, nowy);
  79.             }
  80.         } else if(dzialanie==2){
  81.             int szukany;
  82.             scanf("%d", &szukany);
  83.             wypisywanie(glowa, szukany);
  84.             if(tab[tab_iterator] != szukany){
  85.                 printf("A");
  86.             }else{
  87.                 for(int i = 0; i <= tab_iterator; i++){
  88.                     printf("%d", (int)tab[i]);
  89.                 }
  90.             }
  91.         }
  92.     }while(dzialanie!=0);
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment