Advertisement
Guest User

Untitled

a guest
May 17th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.22 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct element{
  4.     struct element *p;
  5.     int key;
  6.     struct element *left;
  7.     struct element *right;
  8. };
  9. struct element* bst_wstaw(struct element* T,struct element* Z);
  10. struct element* bst_szukaj(struct element* x,int k);
  11. struct element* bst_max(struct element* x);
  12. struct element* bst_min(struct element* x);
  13. void bst_wyswietl(struct element* x);
  14.  
  15. main()
  16. {
  17.     struct element* root=NULL, *nowy=NULL,*max,*min;
  18.     char z;
  19.     int liczba;
  20.     while(1)
  21.     {
  22.         printf("\nWybierz co chcesz zrobic");
  23.         printf("\nd - dodac");
  24.         printf("\ns - szukac");
  25.         printf("\nu - usunac");
  26.         printf("\nw - wyswietlic");
  27.         printf("\nq - wyjscie\n");
  28.         fflush(stdin);
  29.         z=getchar();
  30.         switch(z)
  31.         {
  32.             case 'd':
  33.                 nowy=(struct element*)malloc(sizeof(struct element));
  34.                 printf("\nPodaj wartosc elementu do wstawienia:> ");
  35.                 scanf("%d",&liczba);
  36.                 nowy->key=liczba;
  37.                 nowy->left=NULL;
  38.                 nowy->right=NULL;
  39.                 root=bst_wstaw(root,nowy);
  40.                 break;
  41.             case's':
  42.                 printf("\nPodaj wartosc elementu do wyszukania:> ");
  43.                 scanf("%d",&liczba);
  44.                 nowy=bst_szukaj(root,liczba);
  45.                 if(nowy!=NULL)
  46.                     printf("\nZnaleziono element!");
  47.                 else
  48.                     printf("\nBrak szukanego elementu!");
  49.                 break;
  50.             case 'm':max=bst_max(root)
  51.                          printf("Wartosc maksymalna: %d",max->key);
  52.                 break;
  53.  
  54.             case 'w':
  55.                 printf("\n");
  56.                 bst_wyswietl(root);
  57.                 break;
  58.             case 'q':return 0;
  59.         }
  60.     }
  61. }
  62. struct element* bst_wstaw(struct element* T,struct element* Z)
  63. {
  64.     struct element* x=T, *y=NULL;
  65.     while(x!=NULL)
  66.     {
  67.         y=x;
  68.         if(Z->key < x->key) x=x->left;
  69.         else x=x->right;
  70.     }  
  71.         Z->p=y;
  72.         if(y==NULL) T=Z;
  73.         else if(Z->key < y->key) y->left=Z;
  74.         else y->right=Z;
  75.        
  76.     return T;
  77. }
  78. struct element* bst_szukaj(struct element* x,int k)
  79. {
  80.     if(x==NULL||k==x->key)
  81.         return x;
  82.     if(k < x->key)
  83.         return bst_szukaj(x->left,k);
  84.     return bst_szukaj(x->right,k);
  85. }
  86. struct element* bst_max(struct element* x)
  87. {
  88.     while(x->right!=NULL)
  89.         x=x->right;
  90.     return x;
  91. }
  92. void bst_wyswietl(struct element* x)
  93. {
  94.     if(x!=NULL)
  95.     {
  96.         bst_wyswietl(x->left);
  97.         printf("%d ",x->key);
  98.         bst_wyswietl(x->right);
  99.     }
  100. }
  101. struct element* bst_min(struct element* x)
  102. {
  103.     while(x->left!=NULL)
  104.         x=x->left;
  105.     return x;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement