Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created November 16, 2026 08:01
Show Gist options
  • Select an option

  • Save thinkphp/c19e79b676189c048dfe0f1df32436fd to your computer and use it in GitHub Desktop.

Select an option

Save thinkphp/c19e79b676189c048dfe0f1df32436fd to your computer and use it in GitHub Desktop.
Afiseaza numarul de fruze si frunzele
#include <stdio.h>
#include <stdlib.h>
typedef struct node_type {
int id;
struct node_type *left,
*right;
} NodeT;
//1 7 2 0 5 0 0 0 9 3 0 0 4 0 6 0 0
/*
1
7 9
2 0 3 4
0 5 0 0 0 6
0 0 0 0
*/
NodeT *createBinTreeFromtFile(FILE *f) {
NodeT *p;
int c;
fscanf(f, "%d", &c);
if(c == 0) return NULL;//
p = (NodeT*)malloc(sizeof(NodeT));
if(p == NULL) {
puts("failed allocation");//printf("%s");
exit(1);
}
p->id = c;
p->left = createBinTreeFromtFile(f);
p->right = createBinTreeFromtFile(f);
return p;
}
int leaf_node(NodeT *node) {
if(node == NULL) return 0;
if(node->left == NULL && node->right == NULL) {
printf("%d ", node->id); //afisam frunza
return 1;
}
return leaf_node( node->left ) + leaf_node( node->right );
}
int main(int argc, char const *argv[])
{
NodeT *root = NULL;
FILE *f = fopen("ArboreBinar.txt", "r");
if(!f) {
printf("Nu se poate deschide fisierul ArboreBinar.txt");
return -1;
}
root = createBinTreeFromtFile( f );
fclose( f );
printf("Frunzele arborelui: ");
int nr = leaf_node( root );
printf("\nNumarul de frunze: %d", nr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment