Advertisement
elektryk798

drzewa_lab_03

May 18th, 2017
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.30 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. struct wezel
  5. {
  6.     struct wezel * rodzic, * lewy, * prawy;
  7.     int k;
  8. };
  9.  
  10. struct wezel *BST_WSTAW(struct wezel * root,struct wezel * nowy);
  11. void in_order(struct wezel *root);
  12.  
  13. main()
  14. {
  15.     int ile=0;
  16.     char co='d';
  17.     struct wezel *root=NULL,*nowy=NULL;
  18.     while(co!='x')
  19.     {
  20.         printf("co chcesz zrobic?\nd-dodaj\nw-wyswietl in order\n");
  21.         fflush(stdin);
  22.         scanf("%c",&co);
  23.         switch(co)
  24.         {
  25.             case 'd':
  26.                 nowy=(struct wezel*)malloc(sizeof(struct wezel));
  27.                 nowy->lewy=NULL;
  28.                 nowy->prawy=NULL;
  29.                 fflush(stdin);
  30.                 scanf("%d",&ile);
  31.                 nowy->k=ile;
  32.                 root=BST_WSTAW(root,nowy);
  33.             break;
  34.             case 'w':
  35.                 if(root==NULL)
  36.                     printf("drzewo puste\n");
  37.                 else
  38.                     in_order(root);
  39.             break;
  40.             default:
  41.             break;
  42.         }
  43.     }
  44. }
  45.  
  46. struct wezel *BST_WSTAW(struct wezel * root,struct wezel * nowy)
  47. {
  48.     struct wezel*y=NULL,*x=NULL;
  49.     x=root;
  50.     while(x!=NULL)
  51.     {
  52.         y=x;
  53.         if(nowy->k < x->k)
  54.             x=x->lewy;
  55.         else
  56.             x=x->prawy;
  57.     }
  58.     nowy->rodzic=y;
  59.     if(y==NULL)
  60.         root=nowy;
  61.     else if(nowy->k < y->k)
  62.         y->lewy=nowy;
  63.     else
  64.         y->prawy=nowy;
  65.     return root;
  66. }
  67.  
  68.  
  69. void in_order(struct wezel *root)
  70. {
  71.     if(root)
  72.     {
  73.         in_order(root->lewy);
  74.         printf("%d\n",root->k);
  75.         in_order(root->prawy);
  76.     }
  77. }
  78. void maksimum(struct wezel *root)
  79. {
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement