1. #ifndef HEADER_H
  2. #define HEADER_H
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. typedef struct nod
  7. {
  8.     int inf;
  9.     struct nod *st, *dr;
  10. }NOD;
  11. NOD *CreareArbBinar(FILE *);
  12. void CreareArbBinarCautare(NOD **, int);
  13. void Inordine(NOD *);
  14. void Postordine(NOD *);
  15. void Preordine(NOD *);
  16. #endif
  17.  
  18.  
  19.  
  20. #include "Header.h"
  21. int main()
  22. {
  23.     FILE *f;
  24.     NOD *root = NULL, *root2 = NULL;
  25.     int inf;
  26.     f = fopen("Input.txt", "r");
  27.     root = CreareArbBinar(f);
  28.     fclose(f);
  29.  
  30.     printf("Arborele in preordine:\n");
  31.     Preordine(root);
  32.     printf("\n");
  33.  
  34.     printf("Arborele in postordine:\n");
  35.     Postordine(root);
  36.     printf("\n");
  37.  
  38.     printf("Arborele in inordine:\n");
  39.     Inordine(root);
  40.     printf("\n");
  41.  
  42.     f = fopen("ABC.txt", "r");
  43.     while(!feof(f))
  44.     {
  45.         fscanf(f, "%d", &inf);
  46.         CreareArbBinarCautare(&root2, inf);
  47.     }
  48.     fclose(f);
  49.  
  50.     printf("Parcurgere arbore binar de cautare in inordine:\n");
  51.     Inordine(root2);
  52.     printf("\n");
  53.     return 0;
  54. }
  55.  
  56.  
  57.  
  58.  
  59. #include "Header.h"
  60. NOD *CreareArbBinar(FILE *f)
  61. {
  62.     NOD *p;
  63.     char c[2];
  64.     p = (NOD*)malloc(sizeof(NOD));
  65.     fscanf(f, "%d", &p->inf);
  66.  
  67.     p->dr = p->st = NULL;
  68.  
  69.     fscanf(f, "%s", c);
  70.     if(toupper(c[0]) == 'D')
  71.         p->st = CreareArbBinar(f);
  72.  
  73.     fscanf(f, "%s", c);
  74.     if(toupper(c[0]) == 'D')
  75.         p->dr = CreareArbBinar(f);
  76.     return p;
  77. }
  78.  
  79. void Inordine(NOD *p)
  80. {
  81.     if(p != NULL)
  82.     {
  83.         Inordine(p->st);
  84.         printf("%d ", p->inf);
  85.         Inordine(p->dr);
  86.     }
  87. }
  88.  
  89. void Preordine(NOD *p)
  90. {
  91.     if(p != NULL)
  92.     {
  93.         printf("%d ", p->inf);
  94.         Preordine(p->st);
  95.         Preordine(p->dr);
  96.     }
  97. }
  98.  
  99. void Postordine(NOD *p)
  100. {
  101.     if(p != NULL)
  102.     {
  103.         Postordine(p->st);
  104.         Postordine(p->dr);
  105.         printf("%d ", p->inf);
  106.     }
  107. }
  108.  
  109. void CreareArbBinarCautare(NOD **p, int inf)
  110. {
  111.     if(*p == NULL)
  112.     {
  113.         *p = (NOD*)malloc(sizeof(NOD));
  114.         (*p)->dr = (*p)->st = NULL;
  115.         (*p)->inf = inf;
  116.     }
  117.     else
  118.     {
  119.         if((*p)->inf > inf)
  120.             CreareArbBinarCautare(&((*p)->st), inf);
  121.         else
  122.             CreareArbBinarCautare(&((*p)->dr), inf);
  123.     }
  124. }
  125.  
  126.  
  127.  
  128. 7 5 20 1 6 0 2 11 25 9 15 21 30 --->abc
  129.  
  130. 3 d 4 d 2 n d 5 n n n d 1 d 6 n d 8 d 9 n n n d 7 n n --->input