Advertisement
Guest User

16

a guest
May 24th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.29 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<stdio.h>
  5.  
  6.  
  7. struct node //Структурата която се ползва за бинарно дърво
  8. {
  9.     int info;
  10.     struct node *lchild;
  11.     struct node *rchild;
  12. }*root;
  13.  
  14.  
  15. void find(int item,struct node **par,struct node **loc) // Функция за търсене в дърво
  16. {
  17.     struct node *ptr,*ptrsave;
  18.  
  19.     if(root==NULL)  /*tree empty*/
  20.     {
  21.         *loc=NULL;
  22.         *par=NULL;
  23.         return;
  24.     }
  25.     if(item==root->info) /*item is at root*/
  26.     {
  27.         *loc=root;
  28.         *par=NULL;
  29.         return;
  30.     }
  31.     /*Initialize ptr and ptrsave*/
  32.     if(item<root->info)
  33.         ptr=root->lchild;
  34.     else
  35.         ptr=root->rchild;
  36.     ptrsave=root;
  37.  
  38.     while(ptr!=NULL)
  39.     {
  40.         if(item==ptr->info)
  41.         {       *loc=ptr;
  42.             *par=ptrsave;
  43.             return;
  44.         }
  45.         ptrsave=ptr;
  46.         if(item<ptr->info)
  47.             ptr=ptr->lchild;
  48.         else
  49.             ptr=ptr->rchild;
  50.      }/*End of while */
  51.      *loc=NULL;   /*item not found*/
  52.      *par=ptrsave;
  53. }
  54.  
  55. void insert(int item) //Функция за добавяне на елемент
  56. {       struct node *tmp,*parent,*location;
  57.     find(item,&parent,&location); //Викане на функцията за търсене
  58.     if(location!=NULL)
  59.     {
  60.         printf("Item already present %d\n", item); //Бинарно дърво не може да има дупликации на елементите
  61.         return;
  62.     }
  63.  
  64.     tmp=(struct node *)malloc(sizeof(struct node));
  65.     tmp->info=item;
  66.     tmp->lchild=NULL;
  67.     tmp->rchild=NULL;
  68.  
  69.     if(parent==NULL)
  70.         root=tmp;
  71.     else
  72.         if(item<parent->info)
  73.             parent->lchild=tmp;
  74.         else
  75.             parent->rchild=tmp;
  76. }/*End of insert()*/
  77.  
  78.  
  79. void inorder(struct node *ptr) //Функция за принтиране на дървото сортирано
  80. {
  81.     if(root==NULL)
  82.     {
  83.         printf("Tree is empty");
  84.         return;
  85.     }
  86.     if(ptr!=NULL)
  87.     {
  88.         inorder(ptr->lchild);
  89.         printf("%d  ",ptr->info);
  90.         inorder(ptr->rchild);
  91.     }
  92. }
  93.  
  94.  
  95. void printOdd(struct node *ptr) //Функция за принтиране на нечетните числа
  96. {
  97.     if(ptr != NULL)
  98.     {
  99.         if((ptr->info % 2) != 0)
  100.             printf("%d ", ptr->info);
  101.         printOdd(ptr->lchild);
  102.         printOdd(ptr->rchild);
  103.     }
  104. }
  105.  
  106. void printEven(struct node *ptr) //Функция за принтиране на четните числа
  107. {
  108.     if(ptr != NULL)
  109.     {
  110.         if((ptr->info % 2) == 0)
  111.             printf("%d ", ptr->info);
  112.         printEven(ptr->lchild);
  113.         printEven(ptr->rchild);
  114.     }
  115. }
  116.  
  117.  
  118.  
  119. int main()
  120. {
  121.     insert(10); //Добавяне на елемент (Горе е декларирана глобална променлива която е root-a)
  122.     insert(11);
  123.     insert(4);
  124.     printf("The tree printed sorted:\n");
  125.     inorder(root); //Принтиране на дървото сортирано
  126.     printf("\nEven numbers in the tree:\n");
  127.     printEven(root);//Принтиране на четни числа
  128.     printf("\nOdd numbers in the tree:\n");
  129.     printOdd(root);// Принтиране на нечетни числа
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement