Advertisement
sildren12

drzewa bst

May 14th, 2015
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.60 KB | None | 0 0
  1.    
  2.  
  3.     #include<stdio.h>
  4.     #include<stdlib.h>
  5.      
  6.     struct element
  7.     {
  8.                 int k;
  9.                             struct element *p;
  10.                 struct element *left;
  11.                 struct element *right;
  12.     };
  13.      
  14.     struct element *bst_wstaw(struct element *t, struct element *z);
  15.     void inorder(struct element *x);
  16.     struct element *bst_szukaj(struct element *t, int kl);
  17.     struct element *bst_min(struct element *t);
  18.     struct element *bst_max(struct element *t);
  19.     struct element *bst_poprzednik(struct element *t);
  20.     struct element *bst_nastepnik(struct element *t);
  21.     struct element *bst_usun(struct element *t, struct element *z);
  22.     struct element *bst_zwolnij(struct element *t);
  23.      
  24.     main()
  25.     {
  26.                     struct element *t=NULL, *el=NULL;
  27.                 char z;
  28.                 int liczba;
  29.                 while(1)
  30.                 {                  
  31.                                                     el=NULL;
  32.                             printf("\nco chcesz zrobic?");
  33.                             printf("\nd - dodac");
  34.                             printf("\ns - szukac");
  35.                             printf("\nu - usunac");
  36.                             printf("\nw - wyswietlic");
  37.                                                     printf("\nz - zwolnic");
  38.                             printf("\nq - wyjsc\n");
  39.                             fflush(stdin);
  40.                             z=getchar();
  41.                             switch(z)
  42.                             {
  43.                             case 'd':
  44.                                     el=(struct element*)malloc(sizeof(struct element));
  45.                                     printf("\npodaj wartosc elementu do wstawienia:  ");
  46.                                     scanf("%d",&liczba);
  47.                                     el->k=liczba;
  48.                                                                     el->left=NULL;
  49.                                                                     el->right=NULL;
  50.                                     t=bst_wstaw(t,el);
  51.                                              break;
  52.                             case 'w':    inorder(t);
  53.                                              break;
  54.                             case 'q':     return 0;
  55.                             case 's':
  56.                                 if (t==NULL) printf("\ndrzewo puste ");
  57.                                 else
  58.                                 {printf("\npodaj wartosc do wyszukania:  ");
  59.                                 scanf("%d",&liczba);
  60.                                 el=bst_szukaj(t,liczba);
  61.                                 if (el==NULL) printf("\nnie znaleziono wartosci");
  62.                                 else printf("\nwartosc znaleziono pod adresem %p",el);}
  63.                                 break;
  64.                             case 'u':
  65.                                 if (t==NULL) printf("\ndrzewo puste ");
  66.                                 else
  67.                                 {printf("\npodaj wartosc do usuniecia:  ");
  68.                                 scanf("%d",&liczba);
  69.                                 el=bst_szukaj(t,liczba);
  70.                                 if (el==NULL) printf("\nnie znaleziono elementu w drzewie");
  71.                                 else {t=bst_usun(t,el);
  72.                                 printf("\npoprawnie usunieto");}}
  73.                                 break;
  74.                                                     case 'z':
  75.                                                             t=bst_zwolnij(t);
  76.                                                             break;
  77.                             }
  78.                 }
  79.     }
  80.      
  81.     struct element *bst_wstaw(struct element *t, struct element *z)
  82.     {
  83.             struct element *y=NULL,*x=NULL;
  84.             x=t;
  85.             while(x!=NULL)
  86.             {
  87.                     y=x;
  88.                     if (z->k<x->k) x=x->left;
  89.                     else x=x->right;
  90.             }
  91.             z->p=y;
  92.             if (y==NULL) t=z;
  93.             else
  94.             {
  95.                     if (z->k < y->k) y->left=z;
  96.                     else y->right=z;
  97.             }
  98.             return t;
  99.     }
  100.      
  101.     void inorder(struct element *x)
  102.     {
  103.             if (x!=NULL)
  104.             {
  105.                     inorder(x->left);
  106.                     printf("%d  ",x->k);
  107.                     inorder(x->right);
  108.             }
  109.     }
  110.      
  111.     struct element *bst_szukaj(struct element *t, int kl)
  112.     {
  113.             while(t!=NULL && kl!=t->k)
  114.             {
  115.                     if (kl<t->k) t=t->left;
  116.                     else t=t->right;
  117.             }
  118.             return t;
  119.     }
  120.      
  121.     struct element *bst_min(struct element *t)
  122.     {
  123.             while (t->left!=NULL)
  124.             {
  125.                     t=t->left;
  126.             }
  127.             return t;
  128.     }
  129.      
  130.     struct element *bst_max(struct element *t)
  131.     {
  132.             while (t->right!=NULL)
  133.             {
  134.                     t=t->right;
  135.             }
  136.             return t;
  137.     }
  138.      
  139.     struct element *bst_poprzednik(struct element *t)
  140.     {
  141.             struct element *y=NULL;
  142.             if (t->left!=NULL) return bst_max(t->left);
  143.             y=t->p;
  144.             while (y!=NULL && t==y->left)
  145.             {
  146.                     t=y;
  147.                     y=y->p;
  148.             }
  149.             return y;
  150.     }
  151.      
  152.     struct element *bst_nastepnik(struct element *t)
  153.     {
  154.             struct element *y=NULL;
  155.             if (t->right!=NULL) return bst_min(t->right);
  156.             y=t->p;
  157.             while (y!=NULL && t==y->right)
  158.             {
  159.                     t=y;
  160.                     y=y->p;
  161.             }
  162.             return y;
  163.     }
  164.      
  165.     struct element *bst_usun(struct element *t, struct element *z)
  166.     {
  167.             struct element *y=NULL,*x=NULL;
  168.             int a;
  169.             if (z->left==NULL || z->right==NULL) y=z;
  170.             else y=bst_nastepnik(z);
  171.             if (y->left!=NULL) x=y->left;
  172.             else x=y->right;
  173.             if (x!=NULL) x->p=y->p;
  174.             if (y->p==NULL) t=x;
  175.             else
  176.             {
  177.                     if (y==y->p->left) y->p->left=x;
  178.                     else y->p->right=x;
  179.             }
  180.             if (y!=z)
  181.             {
  182.                     a=y->k;
  183.                     y->k=z->k;
  184.                     z->k=a;
  185.             }
  186.             free(y);
  187.             return t;
  188.     }
  189.      
  190.     struct element *bst_zwolnij(struct element *t)
  191.     {
  192.             while(t!=NULL)
  193.             {
  194.                     t=bst_usun(t,t);
  195.             }
  196.             return t;
  197.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement