Академический Документы
Профессиональный Документы
Культура Документы
(Java)
rvore Binria
Crie a classe ArvoreBinaria em Java com as seguintes funcionalidade:
1. A rvore deve guardar nmeros inteiros.
2. Imprimir de forma crescente os elementos.
3. Imprimir decrescente.
4. Contar o nmero de elementos.
5. Fazer a soma de todos os Elementos.
6. Maior elemento.
7. Menor Elemento.
Nota: O exerccio deve ser implementado recorrendo classe Leitura. Deve ser ainda
implementada a classe ArvoreBinaria e No (que contm um apontador para a
esquerda, e outro para a direita).
RVORES BINRIAS DE PESQUISA
Um tipo especial de rvore binria a rvore binria de pesquisa (ABP). Neste tipo de rvores
cada n tem obrigatoriamente um campo denominado chave que permite impor uma ordenao
na rvore que satisfaz a seguinte propriedade:
Exemplos:
Leitura.java
/*
*Leitura.java
*
*Createdon8deMaiode2006,17:05
*
*Tochangethistemplate,chooseTools|Optionsandlocatethetemplateunder
*theSourceCreationandManagementnode.Rightclickthetemplateandchoose
*Open.YoucanthenmakechangestothetemplateintheSourceEditor.
*/
packageinterfacevector;
/**
*
*@authorAntonioLeslie
*/
importjava.io.*;
publicclassLeitura{
publicintLerInteiro(){
inti=0;
booleanb=true;
BufferedReaderinput=newBufferedReader(newInputStreamReader(System.in));
while(b){
try{
Strings=input.readLine();
i=Integer.valueOf(s.trim()).intValue();
b=false;
}
catch(Exceptione){
System.out.println("Erro...insirauminteirovalido..."+e);
}
}
returni;
}
}
No.java
classNo
{
intvalor;
publicNop_Esq;
publicNop_Dir;
publicNo(intvalor)
{
this.valor=valor;
this.p_Esq=null;
this.p_Dir=null;
}
intObtemValor()
{
returnthis.valor;
}
voidDefineValor(intnovo)
{
this.valor=novo;
}
}
ArvoreBinaria.java
classArvoreBinaria
{
privateNoraiz;
intconta,soma;
intmaior=0;
intmenor=2147483647;//maxint
//construtor
publicArvoreBinaria()
{
this.raiz=null;
}
publicNoObtemRaiz()
{
returnthis.raiz;
}
publicvoidInsOrd(intnovo)
{
Non=newNo(novo);
this.raiz=this.Insere(n,this.raiz);
}
privateNoInsere(Nonovo,Noinicio)
{
If(inicio!=null)
{
If(novo.ObtemValor()<inicio.ObtemValor())
inicio.p_Esq=this.Insere(novo,inicio.p_Esq);
else
inicio.p_Dir=this.Insere(novo,inicio.p_Dir);
returninicio;
}
else
returnnovo;
}
//Ordemcrescente
publicvoidImprimirCrescente(Noraiz)
{
If(raiz!=null)
{
this.ImprimirCrescente(raiz.p_Esq);
System.out.print(raiz.ObtemValor()+",");
this.ImprimirCrescente(raiz.p_Dir);
}
}
//Ordemdecrescente
publicvoidImprimirDecrescente(Noraiz)
{
If(raiz!=null)
{
this.ImprimirDecrescente(raiz.p_Dir);
System.out.print(raiz.ObtemValor()+",");
this.ImprimirDecrescente(raiz.p_Esq);
}
}
publicintContaElementos(Noraiz)
{
If(raiz!=null)
{
conta=this.ContaElementos(raiz.p_Esq)+this.ContaElementos(raiz.p_Dir)+1;
returnconta;
}
else
return0;
}
publicintSomaElementos(Noraiz)
{
If(raiz!=null)
{
soma=this.SomaElementos(raiz.p_Esq)+this.SomaElementos(raiz.p_Dir)+raiz.ObtemValor();
returnsoma;
}
else
return0;
}
publicintMaiorElemento(Noraiz)
{
If(raiz!=null)
{
if(maior<raiz.ObtemValor())
maior=raiz.ObtemValor();
this.MaiorElemento(raiz.p_Esq);
this.MaiorElemento(raiz.p_Dir);
returnmaior;
}
else
return0;
}
publicintMenorElemento(Noraiz)
{
If(raiz!=null)
{
if(menor>raiz.ObtemValor())
menor=raiz.ObtemValor();
this.MenorElemento(raiz.p_Esq);
this.MenorElemento(raiz.p_Dir);
returnmenor;
}
else
return0;
}
}//fimdaclasse
InterfaceTesteA.java
classInterfaceTesteA
{
publicInterfaceTesteA()
{
booleanb=true;
inti;
Leituraoleitura=newLeitura();
ArvoreBinariaoArvore=newArvoreBinaria();
while(b){//seb=0entoterminar
i=oleitura.LerInteiro();
if(i==0)
b=false;
else
{
oArvore.InsOrd(i);
System.out.println("OrdemCrescente:");
oArvore.ImprimirCrescente(oArvore.ObtemRaiz());
System.out.println();
System.out.println("OrdemDecrescente:");
oArvore.ImprimirDecrescente(oArvore.ObtemRaiz());
System.out.println();
intconta=oArvore.ContaElementos(oArvore.ObtemRaiz());
System.out.println("Elementos:"+conta);
intsoma=oArvore.SomaElementos(oArvore.ObtemRaiz());
System.out.println("SomadosElementos:"+soma);
intmaior=oArvore.MaiorElemento(oArvore.ObtemRaiz());
System.out.println("ElementoMaior:"+maior);
intmenor=oArvore.MenorElemento(oArvore.ObtemRaiz());
System.out.println("ElementoMenor:"+menor);
}
}
}
publicstaticvoidmain(StringArgs[])
{
newInterfaceTeste();
}
}//fimdaclasse