Baixe o app para aproveitar ainda mais
Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> struct tree{ char dado[50]; struct tree *left; struct tree *right; }; struct tree *rt; /* primeiro nó da árvore */ struct tree *stree(struct tree *r); void print_tree(struct tree *root,int l); char letra(){ char info; printf("\nEntre com uma letra: "); fflush(stdin); info = getch(); return info; } struct tree *stree(struct tree *r){ char info[50]; printf("dado: "); fflush(stdin); gets(info); if(!r){ r = (struct tree *)malloc(sizeof(struct tree)); if(!r){ printf("sem memoria\n"); exit(0); } r->left = NULL; r->right = NULL; if(strcmpi(info,".") != 0){ strcpy(r->dado,info); r->left = stree(r->left); r->right = stree(r->right); return r; } else{ r = NULL; return r; } } return r; } void inorder(struct tree *root){ if(!root) return; inorder(root->left); if(root->dado) printf("%s\n",root->dado); inorder(root->right); } void preorder(struct tree *root){ if(!root)return; if(root->dado) printf("%s\n",root->dado); preorder(root->left); preorder(root->right); } void postorder(struct tree *root){ if(!root) return; postorder(root->left); postorder(root->right); if(root->dado)printf("%s\n",root->dado); } void print_tree(struct tree *r,int l){ int i; if(r == NULL) return; print_tree(r->right,l+1); for(i = 0;i<l;i++) printf(" "); printf(".%s.\n",r->dado); print_tree(r->left,l+1); } int main(){ rt = NULL; rt = stree(rt); system ("pause"); printf("\n\n"); printf(" IMPRIMINDO A ARVORE\n\n"); print_tree(rt,0); printf("\n\n"); system ("pause"); printf("\n\n"); printf(" INORDER\n\n"); inorder(rt); printf("\n\n"); system ("pause"); printf("\n\n"); printf(" PREORDER\n\n"); preorder(rt); printf("\n\n"); system ("pause"); printf("\n\n"); printf(" POSTORDER\n\n"); postorder(rt); printf("\n\n"); return 0; }
Compartilhar