Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct arvore{
- int elemento;
- struct arvore *dir;
- struct arvore*esq;
- };
- typedef struct arvore arvore;
- arvore *aloca (int x)
- {
- arvore *novo = (arvore*) malloc(sizeof(arvore));
- novo->dir = NULL;
- novo->esq = NULL;
- novo->elemento = x;
- return novo;
- }
- void inserir(arvore *p, int x)
- {
- if(x > p->elemento)
- {
- if(p->dir == NULL)
- {
- p->dir = aloca(x);
- }
- else
- {
- inserir(p->dir,x);
- }
- }
- else if( x < p->elemento)
- {
- if(p->esq == NULL)
- {
- p->esq = aloca(x);
- }
- else
- {
- inserir(p->esq, x);
- }
- }
- }
- void mostrar_em_ordem(arvore *p)
- {
- if(p == NULL)
- {
- return;
- }
- mostrar_em_ordem(p->esq);
- printf("%d ",p->elemento);
- mostrar_em_ordem(p->dir);
- }
- int main()
- {
- arvore arv1;
- arv1.dir = NULL;
- arv1.esq = NULL;
- printf("Digite a raiz da arvore: ");
- scanf("%d",&arv1.elemento);
- inserir(&arv1,3);
- inserir(&arv1,1);
- inserir(&arv1,8);
- inserir(&arv1,2);
- inserir(&arv1,4);
- inserir(&arv1,0);
- mostrar_em_ordem(&arv1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement