Facebook
From Rude Coyote, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 244
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct element{
  5.         int k;
  6.         struct element *parent;
  7.         struct element *lewy;
  8.         struct element *prawy;
  9. };
  10.  
  11. struct element * bst_wstaw(struct element *root, struct element *nowy);
  12. void inorder(struct element *x);
  13. struct element *bst_min(struct element *x);
  14. struct element *bst_max(struct element *x);
  15. struct element *bst_szukaj(struct element *x, int key);
  16.  
  17. int main(){
  18.         struct element * root = NULL;
  19.         struct element * nowy = NULL;
  20.         int liczba;
  21.         char z;
  22.  
  23.         while(1){
  24.                 printf("\nCo zrobic: ");
  25.                 fflush(stdin);
  26.                 z = getchar();
  27.  
  28.                 switch(z){
  29.                         case 'd':
  30.                                 nowy = (struct element*) malloc (sizeof(struct element));
  31.                                 printf("\nPodaj wartosc: ");
  32.                                 fflush(stdin);
  33.                                 scanf("%d",&liczba);
  34.                                 nowy->k = liczba;
  35.                                 //!!!!!!!!!!!
  36.                                 nowy->lewy = NULL;
  37.                                 nowy->prawy = NULL;
  38.                                 root = bst_wstaw(root, nowy);
  39.                                 break;
  40.                        
  41.                         case 'n':
  42.                                 nowy = bst_min(root);
  43.                                 liczba = nowy->k;
  44.                                 printf("\nMin: %d", liczba);
  45.                                 break;
  46.  
  47.                         case 'm':
  48.                                 nowy = bst_max(root);
  49.                                 liczba = nowy->k;
  50.                                 printf("\nMax: %d", liczba);
  51.                                 break;
  52.  
  53.                         case 'w':
  54.                                 inorder(root);
  55.                                 break;
  56.                        
  57.                         case 's':
  58.                                 printf("\nCo znalezc: ");
  59.                                 fflush(stdin);
  60.                                 scanf("%d",&liczba);
  61.                                 nowy = bst_szukaj(root, liczba);
  62.                                 if(nowy != NULL)
  63.                                         printf("\nZnaleziono na adresie: %p",nowy);
  64.                                 else
  65.                                         printf("\nNie znaleziono!");
  66.                                 break;
  67.  
  68.                         case 'q':
  69.                                 return 0;
  70.                 }
  71.         }
  72. }
  73.  
  74. struct element * bst_wstaw(struct element *root, struct element *nowy){
  75.         struct element *y = NULL;
  76.         struct element *x;
  77.  
  78.         x = root;
  79.  
  80.         while(x!=NULL){
  81.                 y = x;
  82.                 if (nowy->k < x->k)
  83.                         x = x->lewy;
  84.                 else
  85.                         x = x->prawy;
  86.         }
  87.                 nowy->parent = y;
  88.                 if (y == NULL)
  89.                         root = nowy;
  90.                 else if(nowy->k < y->k)
  91.                         y->lewy = nowy;
  92.                 else
  93.                         y->prawy = nowy;
  94.         return root;
  95. }
  96.  
  97. void inorder(struct element *x){
  98.         if (x!=NULL){
  99.                 inorder(x->lewy);
  100.                 printf("%d\t",x->k);
  101.                 inorder(x->prawy);
  102.         }
  103. }
  104.  
  105. struct element *bst_min(struct element *x){
  106.         while(x->lewy !=NULL)
  107.                 x = x->lewy;
  108.         return x;
  109. }
  110.  
  111. struct element *bst_max(struct element *x){
  112.         while(x->prawy !=NULL)
  113.                 x = x->prawy;
  114.         return x;
  115. }
  116.  
  117. struct element *bst_szukaj(struct element *x, int key){
  118.         while((x != NULL) && (x->k != key)){
  119.                 if (key < x->k)
  120.                         x = x->lewy;
  121.                 else
  122.                         x = x->prawy;
  123.         }
  124.         return x;
  125. }