Árvore
Código em Java, não recursivo:
package arvore;
import java.util.Scanner; public class abnr {
private static class arvore{ public int num;
public arvore sad, sae; }
private static class pilha{ public arvore num; public pilha prox; }
public static void main(String args[]){ Scanner leia = new Scanner(System.in); arvore raiz = null;
arvore aux; arvore aux1; arvore novo; arvore anterior; pilha topo; pilha aux_pilha; int op, achou, numero; do{
System.out.println("\nMenu de Opcoes\n"); System.out.println("1 - Inserir na arvore");
System.out.println("2 - Consultar um no da arvore");
System.out.println("3 - Consultar toda a arvore em ordem"); System.out.println("4 - Consultar toda a arvore em pre-ordem"); System.out.println("5 - Consultar toda a arvore em pos-ordem"); System.out.println("6 - Excluir um no da arvore");
System.out.println("7 - Esvaziar a arvore"); System.out.println("8 - Sair");
System.out.print("Digite sua opcao: "); op = leia.nextInt();
if (op < 1 || op > 8)
System.out.println("Opcao invalida"); if (op == 1){
System.out.println("Digite o numero a ser inserido: "); novo = new arvore();
novo.num = leia.nextInt(); novo.sad = null; novo.sae = null; if (raiz == null) raiz = novo; else{ aux = raiz; achou = 0; while (achou == 0){ if (novo.num < aux.num){ if (aux.sae == null){ aux.sae = novo; achou = 1; } else aux = aux.sae; }
else if (novo.num >= aux.num){ if (aux.sad == null){ aux.sad = novo; achou = 1; } else aux = aux.sad;
} }
System.out.println("Numero inserido na arvore"); } } if (op == 2){ if (raiz == null){ System.out.println("Arvore vazia"); } else{
System.out.println("Digite o elemento a ser consultado");
numero = leia.nextInt(); achou = 0;
aux = raiz;
while (aux != null && achou == 0){ if (aux.num == numero){
System.out.print("Numero encontrado"); achou = 1;
}
else if (numero < aux.num) aux = aux.sae;
else
aux = aux.sad; }
if (achou == 0)
System.out.println("Numero nao encontrado"); } } if (op == 3){ if (raiz == null){ System.out.println("Arvore vazia"); } else { System.out.println("Listando..."); aux = raiz; topo = null; do {
while (aux != null){
aux_pilha = new pilha(); aux_pilha.num = aux; aux_pilha.prox = topo; topo = aux_pilha; aux = aux.sae; } if (topo != null){ aux_pilha = topo; System.out.print(aux_pilha.num.num+" "); aux = topo.num.sad; topo = topo.prox; }
}while (topo != null || aux != null); } } if (op == 4){ if (raiz == null){ System.out.println("Arvore vazia"); } else{ System.out.println("Listando..."); aux = raiz; topo = null; do{ while(aux != null){
aux_pilha = new pilha(); System.out.print(aux.num+" "); aux_pilha.num = aux;
topo = aux_pilha; aux = aux.sae; } if (topo != null){ aux_pilha = topo; topo = topo.prox; aux = aux_pilha.num.sad; }
}while (topo != null || aux != null); } } if (op == 5){ if (raiz == null){ System.out.println("Arvore vazia"); } else { System.out.println("Listando..."); aux = raiz; topo = null; do{ do{
while (aux != null){
aux_pilha = new pilha(); aux_pilha.num = aux; aux_pilha.prox = topo; topo = aux_pilha; aux = aux.sae; } if (topo.num.sad != null) aux = topo.num.sad; }while (aux != null); if (topo != null){
System.out.print(topo.num.num+" "); if (topo.prox != null){
if(topo.prox.num.sad != null &&
topo.prox.num.sad != topo.num){ aux = topo.prox.num.sad;
topo = topo.prox; }
else{
while (topo.prox != null && topo.prox.num.sad == topo.num){ topo = topo.prox; System.out.print( topo.num.num+" "); } topo = topo.prox; if (topo != null) aux = topo.num.sad; else aux = null; } } else{ topo = topo.prox; aux = null; } }
}while (topo != null || aux != null); } } if (op == 6){ if (raiz == null) System.out.println("Arvore vazia"); else{
System.out.println("Digite o numero que deseja excluir");
numero = leia.nextInt(); aux = raiz;
achou = 0;
while (achou == 0 && aux != null){ if (aux.num == numero)
achou = 1;
else if (aux.num > numero) aux = aux.sae;
else
aux = aux.sad; }
if (achou == 0)
System.out.println("Numero nao encontrado"); else{
if (aux != raiz){ anterior = raiz;
while (anterior.sad != aux && anterior.sae != aux){ if (anterior.num > numero) anterior = anterior.sae; else anterior = anterior.sad; }
if (aux.sad == null && aux.sae == null){ if (anterior.sad == aux) anterior.sad = null; else anterior.sae = null; } else{
if (aux.sad != null && aux.sae == null){ if (anterior.sae == aux)
anterior.sae = aux.sad; }
else if (aux.sae != null && aux.sad == null){ if (anterior.sae == aux) anterior.sae = aux.sae; else anterior.sad = aux.sae; }
else if (aux.sae != null && aux.sad != null){ if (anterior.sad == aux){ anterior.sad = aux.sad; aux1 = aux.sae; } else{ anterior.sae = aux.sae; aux1 = aux.sad; } aux = anterior; while (aux != null){
if (aux.num < aux1.num){ if (aux.sad == null){ aux.sad = aux1; aux = null; }
else aux = aux.sad; }
else if (aux.num > aux1.num){ if (aux.sae == null){ aux.sae = aux1; aux = null; }
else aux = aux.sae; }
}
} }
else {
if (aux.sad == null && aux.sae == null) raiz = null;
else {
if (aux.sad != null && aux.sae == null)
raiz = aux.sad; else if (aux.sae != null && aux.sad ==
null)
raiz = aux.sae;
else if (aux.sae != null && aux.sad != null){
raiz = aux.sad; aux1 = aux.sae; aux = raiz;
while (aux != null){
if (aux.num < aux1.num){ if (aux.sad == null){ aux.sad = aux1; aux = null; }
else aux = aux.sad; }
else if(aux.num > aux1.num){ if (aux.sae == null){ aux.sae = aux1; aux = null; } else aux = aux.sae; } } } } } System.out.println("Numero excluido"); } } } if (op == 7){ if (raiz == null) System.out.println("Arvore vazia"); else{ raiz = null; System.out.println("Arvore esvaziada"); } } }while (op != 8); } }