Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- typedef struct tipoNo{
- int valor;
- struct tipoNo *esq;
- struct tipoNo *dir;
- }TNo;
- TNo* inserir (TNo *raiz, int valorParametro){
- if (raiz == NULL){
- raiz = new TNo; // criação de um novo no
- raiz->valor = valorParametro;
- raiz->dir = NULL;
- raiz->esq = NULL;
- } else{
- if (valorParametro < raiz->valor){
- raiz->esq = inserir(raiz->esq ,valorParametro);
- } else{
- raiz->dir = inserir(raiz->dir ,valorParametro);
- }
- }
- return raiz;
- }
- int consultaRecursiva (TNo *raiz ,int itemConsulta){
- if (raiz == NULL){
- printf("Sua Arvore esta vazia !! n");
- system("pause");
- return 0;
- } else {
- if (raiz->valor == itemConsulta){
- printf("Achou !!!!");
- system("pause");
- return 1 ;
- } else{
- if (raiz->valor > itemConsulta){
- return consultaRecursiva(raiz->esq ,itemConsulta);
- } else{
- return consultaRecursiva(raiz->dir ,itemConsulta);
- }
- }
- }
- }
- void preOrdem (TNo *raiz_aux){
- if (raiz_aux != NULL){
- printf("%d", raiz_aux->valor);
- preOrdem(raiz_aux->esq);
- preOrdem(raiz_aux->dir);
- system("pause");
- }
- }
- int main (){
- TNo *raiz; // ponteiro do tipo NO
- raiz = NULL; // a raiz DEVE estar nula
- int op;
- do {
- system("cls");
- printf ("INFORME UMA OPCAO ");
- printf ("nPara inserir digite 1 ");
- printf ("nPara consultar digite 2 ");
- printf ("nPara preOrdem digite 3 n");
- printf ("Para sair digite 0 : ");
- scanf ("%d",&op);
- if (op == 1){
- int novoValor;
- printf("Entre com um novo valor : ");
- scanf ("%d",&novoValor);
- inserir (raiz ,novoValor);
- }
- if (op == 2){
- int itemPesquisa;
- printf("Entre com uma elemento para a busca : ");
- scanf ("%d",&itemPesquisa);
- consultaRecursiva(raiz ,itemPesquisa);
- }
- if (op == 3){
- preOrdem(raiz);
- }
- }while(op != 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement