Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct noarvore {
- struct noarvore* esq;
- int valor ;
- struct noarvore* dir;
- };
- typedef struct noarvore No;
- typedef No* ArvoreBinaria;
- void criarArvore(ArvoreBinaria *arvore){
- *arvore = NULL;
- };
- void inserir(ArvoreBinaria *arvore, int novovalor){
- if((*arvore) == NULL){
- *arvore = (No*) malloc(sizeof(No));
- (*arvore)-> esq = NULL;
- (*arvore)-> dir = NULL;
- (*arvore)-> valor = novovalor;
- }
- if (novovalor < (*arvore)->valor){
- inserir((&(*arvore)->esq),novovalor);
- }
- else {
- if (novovalor > (*arvore)->valor){
- inserir(&((*arvore)-> dir),novovalor);
- }
- }
- }
- void prefixado(ArvoreBinaria no){
- if (no != NULL){
- printf("%d\n", no ->valor);
- if (no->esq != MULL) prefixado(no->esq);
- if (no->dir != NULL) prefixado(no->dir);
- }
- }
- void central(ArvoreBinaria no){
- if (no != NULL){
- if (no-> != NULL) central (no->dir esq);
- printf("$d\n", no->valor);
- if (no->dir != NULL) central (no ->dir);
- }
- }
- void posfixado(ArvoreBinaria no){
- if (no != NULL){
- if (no->esq != NULL) posfixado(no->esq);
- if (no->dir != NULL) posfixado(no->dir);
- }
- }
- void menu(){
- ArvoreBinaria binaria;
- criarArvore(&binaria);
- int op = 0,valor;
- do{
- system("CLS");
- printf("1- Inserir\n2-Listar CENTRAL\n4-Listar POS\n5-Sair\nEscolha uma opcao:");
- scanf("%d", &op);
- switch(op){
- case 1:printf("valor:\n");
- scanf("%d",&valor);
- inserir(&binaria,valor);
- break;
- case 2:prefixado(binaria);
- system ("pause");
- break;
- case 3:central(binaria);
- system("pause");
- break;
- case 4:posfixado(binaria);
- system("pause");
- break;
- }
- }while(op != 5);
- }
- int main(int argc, char *argv[])
- {
- menu();
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement