ArvoreBinaria.pdf
-
Upload
marcos-rios -
Category
Documents
-
view
11 -
download
0
Transcript of ArvoreBinaria.pdf
![Page 1: ArvoreBinaria.pdf](https://reader035.fdocumentos.com/reader035/viewer/2022081210/55cf994b550346d0339ca0ab/html5/thumbnails/1.jpg)
Folha Nº 5. (Java)
Árvore Binária Crie a classe ArvoreBinaria em Java com as seguintes funcionalidade:
1. A árvore deve guardar números inteiros.
2. Imprimir de forma crescente os elementos.
3. Imprimir decrescente.
4. Contar o número de elementos.
5. Fazer a soma de todos os Elementos.
6. Maior elemento.
7. Menor Elemento.
Nota: O exercício deve ser implementado recorrendo à classe Leitura. Deve ser ainda
implementada a classe ArvoreBinaria e No (que contém um apontador para a
esquerda, e outro para a direita).
![Page 2: ArvoreBinaria.pdf](https://reader035.fdocumentos.com/reader035/viewer/2022081210/55cf994b550346d0339ca0ab/html5/thumbnails/2.jpg)
ÁRVORES BINÁRIAS DE PESQUISA Um tipo especial de árvore binária é a árvore binária de pesquisa (ABP). Neste tipo de árvores cada nó tem obrigatoriamente um campo denominado chave que permite impor uma ordenação na árvore que satisfaz a seguinte propriedade: Seja x um nó numa ABP. Se y é um nó na subárvore esquerda de x então
chave[y] ≤ chave[x]
Se y é um nó na subárvore direita de x então chave[x] < chave[y]
Exemplos:
![Page 3: ArvoreBinaria.pdf](https://reader035.fdocumentos.com/reader035/viewer/2022081210/55cf994b550346d0339ca0ab/html5/thumbnails/3.jpg)
Leitura.java /* * Leitura.java * * Created on 8 de Maio de 2006, 17:05 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right‐click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package interfacevector; /** * * @author Antonio Leslie */ import java.io.*; public class Leitura { public int LerInteiro() { int i=0; boolean b=true; BufferedReader input = new BufferedReader ( new InputStreamReader(System.in)); while (b) { try { String s = input.readLine(); i = Integer.valueOf(s.trim()).intValue(); b=false; } catch (Exception e) { System.out.println("Erro... insira um inteiro valido..."+e); } } return i; } }
![Page 4: ArvoreBinaria.pdf](https://reader035.fdocumentos.com/reader035/viewer/2022081210/55cf994b550346d0339ca0ab/html5/thumbnails/4.jpg)
No.java class No { int valor; public No p_Esq; public No p_Dir; public No(int valor) { this.valor = valor; this.p_Esq = null; this.p_Dir = null; } int ObtemValor() { return this.valor; } void DefineValor(int novo) { this.valor = novo; } }
![Page 5: ArvoreBinaria.pdf](https://reader035.fdocumentos.com/reader035/viewer/2022081210/55cf994b550346d0339ca0ab/html5/thumbnails/5.jpg)
ArvoreBinaria.java class ArvoreBinaria { private No raiz; int conta, soma; int maior = 0; int menor = 2147483647; //maxint //construtor public ArvoreBinaria() { this.raiz = null; } public No ObtemRaiz() { return this.raiz; } public void InsOrd(int novo) { No n = new No(novo); this.raiz = this.Insere(n, this.raiz); } private No Insere(No novo, No inicio) { 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); return inicio; } else return novo; }
![Page 6: ArvoreBinaria.pdf](https://reader035.fdocumentos.com/reader035/viewer/2022081210/55cf994b550346d0339ca0ab/html5/thumbnails/6.jpg)
// Ordem crescente public void ImprimirCrescente(No raiz) { If (raiz != null) { this.ImprimirCrescente(raiz.p_Esq); System.out.print(raiz.ObtemValor() + ", "); this.ImprimirCrescente(raiz.p_Dir); } } // Ordem decrescente public void ImprimirDecrescente(No raiz) { If (raiz != null) { this.ImprimirDecrescente(raiz.p_Dir); System.out.print(raiz.ObtemValor() + ", "); this.ImprimirDecrescente(raiz.p_Esq); } } public int ContaElementos(No raiz) { If (raiz != null) { conta=this.ContaElementos(raiz.p_Esq)+this.ContaElementos(raiz.p_Dir)+1; return conta; } else return 0; } public int SomaElementos(No raiz) { If (raiz != null) { soma=this.SomaElementos(raiz.p_Esq)+this.SomaElementos(raiz.p_Dir)+raiz.ObtemValor(); return soma; } else return 0; }
![Page 7: ArvoreBinaria.pdf](https://reader035.fdocumentos.com/reader035/viewer/2022081210/55cf994b550346d0339ca0ab/html5/thumbnails/7.jpg)
public int MaiorElemento(No raiz) { If (raiz != null) { if (maior < raiz.ObtemValor()) maior = raiz.ObtemValor(); this.MaiorElemento(raiz.p_Esq); this.MaiorElemento(raiz.p_Dir); return maior; } else return 0; } public int MenorElemento(No raiz) { If (raiz != null) { if (menor > raiz.ObtemValor()) menor = raiz.ObtemValor(); this.MenorElemento(raiz.p_Esq); this.MenorElemento(raiz.p_Dir); return menor; } else return 0; } } // fim da classe
![Page 8: ArvoreBinaria.pdf](https://reader035.fdocumentos.com/reader035/viewer/2022081210/55cf994b550346d0339ca0ab/html5/thumbnails/8.jpg)
InterfaceTesteA.java
class InterfaceTesteA { public InterfaceTesteA() { boolean b = true; int i; Leitura oleitura = new Leitura(); ArvoreBinaria oArvore = new ArvoreBinaria(); while ( b ) { // se b=0 então terminar i = oleitura.LerInteiro(); if (i == 0) b = false; else { oArvore.InsOrd(i); System.out.println("Ordem Crescente:"); oArvore.ImprimirCrescente(oArvore.ObtemRaiz()); System.out.println(); System.out.println("Ordem Decrescente:"); oArvore.ImprimirDecrescente(oArvore.ObtemRaiz()); System.out.println(); int conta = oArvore.ContaElementos(oArvore.ObtemRaiz()); System.out.println("Elementos: " + conta); int soma = oArvore.SomaElementos(oArvore.ObtemRaiz()); System.out.println("Soma dos Elementos: " + soma); int maior = oArvore.MaiorElemento(oArvore.ObtemRaiz()); System.out.println("Elemento Maior: " + maior); int menor = oArvore.MenorElemento(oArvore.ObtemRaiz()); System.out.println("Elemento Menor: " + menor); } } } public static void main(String Args[]) { new InterfaceTeste(); }
} //fim da classe