Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Arvore;
- public class Arvore {
- private No raiz;
- private No folha;
- private int quantidade;
- public Arvore() {
- this.raiz = null;
- this.folha = folha;
- this.quantidade = 0;
- }
- public int getQuantidade() {
- return quantidade;
- }
- public void setQuantidade(int quantidade) {
- this.quantidade = quantidade;
- }
- public boolean haveFilha() {
- return raiz.getDireita() != null || raiz.getEsquerda() != null;
- }
- public boolean IsEmpty() {
- return raiz == null;
- }
- public boolean isEmptyE() {
- return raiz.getEsquerda() == null;
- }
- public boolean isEmptyD() {
- return raiz.getDireita() == null;
- }
- public void inserir(int elemento) {
- No novo = new No(elemento);
- if (IsEmpty()) {
- this.raiz = novo;
- }
- if (!haveFilha()) {
- if (novo.getElemento() < raiz.getElemento()) {
- raiz.setEsquerda(novo);
- } else {
- raiz.setDireita(novo);
- }
- if (haveFilha()) {
- if (novo.getElemento() > raiz.getElemento()) {
- if (!isEmptyD()) {
- if (raiz.getDireita().getElemento() < novo
- .getElemento()) {
- raiz.getDireita().setDireita(novo);
- } else {
- raiz.getDireita().setEsquerda(novo);
- }
- if (raiz.getDireita().getElemento() > novo
- .getElemento()) {
- raiz.getDireita().setEsquerda(novo);
- }
- if (novo.getElemento() < raiz.getElemento()) {
- if (!isEmptyE()) {
- if (novo.getElemento() > raiz.getEsquerda()
- .getElemento()) {
- raiz.getEsquerda().setDireita(novo);
- } else if (novo.getElemento() < raiz
- .getEsquerda().getElemento()) {
- raiz.getEsquerda().setEsquerda(novo);
- }
- }
- }
- }
- }
- }
- }
- quantidade++;}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement