Facebook
From informatyk, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 249
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. struct element{
  5. struct element *parent;
  6. int k;
  7. struct element *right;
  8. struct element *left;
  9. };
  10.  
  11. struct element *BST_wstaw(struct element *root,struct element *nowy);
  12. void inorder(struct element *root);
  13.  
  14. int main(){
  15. struct element *root=NULL;
  16. struct element *nowy=NULL;
  17. int liczba,i=0;
  18. while(i<5){
  19.         printf("Podaj lliczbe");
  20.         scanf("%d",&liczba);
  21.         nowy = (struct element*)malloc(sizeof(struct wezel));
  22.         nowy->k=liczba;
  23.         nowy->left=NULL;
  24.         nowy->right=NULL;
  25.         root=BST_wstaw(root,nowy);
  26.         i++;
  27. }
  28. inorder(root);
  29.  
  30. getch();
  31. return 0;
  32. }
  33.  
  34. struct element* BST_wstaw(struct element *root,struct element *nowy){
  35.        
  36.         struct element* y= NULL,*x=root;
  37.         while(x!=NULL){
  38.                 y=x;
  39.                 if(x->k < nowy->k) x=x->right;
  40.                 else x=x->left;
  41.         }
  42.         nowy -> parent = y;
  43.         if(y==NULL) root=nowy;
  44.         else if(nowy->k < y-> k) y->left=nowy;
  45.         else y->right=nowy;
  46.         return root;
  47. }
  48. void inorder(struct wezel *root){
  49.         if(root!=NULL){
  50.                 inorder(root->left);
  51.                 printf("%d ",root->k);
  52.                 inorder(root->right);
  53.         }
  54. }
  55. struct element* usun(struct element *root,struct element *nowy)
  56. {
  57.         struct element* y;
  58.         if(nowy->left=NULL || nowy->right=NULL)
  59.                 y=nowy;
  60.         else y=nastepnik(root);
  61.         if(y->left!=NULL)
  62.                 x=y=>left;
  63.         else x=y->right;
  64.         if(x!=NULL)
  65.                 x->parent=y->parent;
  66.         if y->p=NULL
  67.  
  68.  
  69.  
  70.  
  71. struct element* nastepnik(struct element*x)
  72. {
  73.         struct element* y;
  74.         if x->right!=NULL;
  75.         return min(x->right);
  76.         y=x->parent;
  77.         while(y!=NULL && x==y->right)
  78.         {
  79.                 x=y;
  80.                 y=y->parent;
  81.         }
  82.         return y;
  83. }
  84.  
  85.                
  86.  
  87.  
  88. struct element* min(struct element*x)
  89. {
  90.         if (x->left==NULL)
  91.                 return x;
  92.         else return min(x->left);
  93. }