Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef HEADER_H
- #define HEADER_H
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- typedef struct nod
- {
- int inf;
- struct nod *st, *dr;
- }NOD;
- NOD *CreareArbBinar(FILE *);
- void CreareArbBinarCautare(NOD **, int);
- void Inordine(NOD *);
- void Postordine(NOD *);
- void Preordine(NOD *);
- #endif
- #include "Header.h"
- int main()
- {
- FILE *f;
- NOD *root = NULL, *root2 = NULL;
- int inf;
- f = fopen("Input.txt", "r");
- root = CreareArbBinar(f);
- fclose(f);
- printf("Arborele in preordine:\n");
- Preordine(root);
- printf("\n");
- printf("Arborele in postordine:\n");
- Postordine(root);
- printf("\n");
- printf("Arborele in inordine:\n");
- Inordine(root);
- printf("\n");
- f = fopen("ABC.txt", "r");
- while(!feof(f))
- {
- fscanf(f, "%d", &inf);
- CreareArbBinarCautare(&root2, inf);
- }
- fclose(f);
- printf("Parcurgere arbore binar de cautare in inordine:\n");
- Inordine(root2);
- printf("\n");
- return 0;
- }
- #include "Header.h"
- NOD *CreareArbBinar(FILE *f)
- {
- NOD *p;
- char c[2];
- p = (NOD*)malloc(sizeof(NOD));
- fscanf(f, "%d", &p->inf);
- p->dr = p->st = NULL;
- fscanf(f, "%s", c);
- if(toupper(c[0]) == 'D')
- p->st = CreareArbBinar(f);
- fscanf(f, "%s", c);
- if(toupper(c[0]) == 'D')
- p->dr = CreareArbBinar(f);
- return p;
- }
- void Inordine(NOD *p)
- {
- if(p != NULL)
- {
- Inordine(p->st);
- printf("%d ", p->inf);
- Inordine(p->dr);
- }
- }
- void Preordine(NOD *p)
- {
- if(p != NULL)
- {
- printf("%d ", p->inf);
- Preordine(p->st);
- Preordine(p->dr);
- }
- }
- void Postordine(NOD *p)
- {
- if(p != NULL)
- {
- Postordine(p->st);
- Postordine(p->dr);
- printf("%d ", p->inf);
- }
- }
- void CreareArbBinarCautare(NOD **p, int inf)
- {
- if(*p == NULL)
- {
- *p = (NOD*)malloc(sizeof(NOD));
- (*p)->dr = (*p)->st = NULL;
- (*p)->inf = inf;
- }
- else
- {
- if((*p)->inf > inf)
- CreareArbBinarCautare(&((*p)->st), inf);
- else
- CreareArbBinarCautare(&((*p)->dr), inf);
- }
- }
- 7 5 20 1 6 0 2 11 25 9 15 21 30 --->abc
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement