Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef char* TElemento ;
- typedef struct nodo{
- TElemento nombre;
- int num;
- struct nodo *left;
- struct nodo *right;
- }TNodo;
- typedef struct arbol{
- TNodo *root;
- }TArbol;
- void tree_init(TArbol *);
- void tree_insert(TArbol *,TElemento);
- int tree_empty(TArbol *);
- void preorder(TNodo *);
- void tree_init(TArbol *T){
- T->root=NULL;
- }
- int tree_empty(TArbol *T){
- return (T->root==NULL);
- }
- void preorder(TNodo *N){
- if (N==NULL)
- return;
- printf("%d\n",N->num);
- preorder(N->left);
- preorder(N->right);
- }
- void tree_insert(TArbol *T,TElemento elem){
- TNodo *crt=T->root;
- TNodo *ant;
- int flag=-1;
- while (crt){
- printf("Raíz %s\n",T->root->nombre);
- if (crt->nombre==elem){
- printf("%s es igual a %s\n",crt->nombre,elem);
- break;
- }
- else{
- ant=crt;
- if (crt->nombre>elem){
- flag=1;
- crt=crt->right;
- }
- else{
- flag=0;
- crt=crt->left;
- }
- }
- }
- if (crt==NULL){
- printf("No se encontró %s en el arbol\n",elem);
- TNodo *nuevo=malloc(sizeof(TNodo));
- nuevo->nombre=elem;
- nuevo->num=1;
- nuevo->left=NULL;
- nuevo->right=NULL;
- if (crt==T->root){
- printf("Es raíz %s\n",elem);
- T->root=nuevo;
- }
- else{
- if (flag)
- ant->right=nuevo;
- else
- ant->left=nuevo;
- printf("El hoja\n");
- }
- printf("%s aparece %d veces\n",elem,nuevo->num);
- }
- else if (crt->nombre==elem){
- printf("Se encontró %s en el arbol\n",elem);
- (crt->num)++;
- printf("%s aparece %d veces\n",elem,crt->num);
- }
- }
- int main(){
- int ncasos;
- TArbol T;
- char* s=malloc(sizeof(30));
- scanf("%d\n",&ncasos);
- tree_init(&T);
- while(ncasos--){
- while(gets(s)&&s[0]){
- tree_insert(&T,s);
- printf("Raíz %s\n",T.root->nombre);
- }
- preorder(T.root);
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment