fefetl08

ArvoreBinaria

May 9th, 2020
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. public class ArvoreBinaria {
  2.     private Node raiz;
  3.  
  4.     public ArvoreBinaria() {
  5.         this.raiz = null;
  6.     }
  7.     private boolean vazia() {
  8.         return this.raiz == null;
  9.     }
  10.  
  11.     public void insereElemento(int info) {
  12.         Node folha = new Node(info);
  13.         if (vazia()) this.raiz = folha;
  14.  
  15.         else {
  16.             ArvoreBinaria arvore = new ArvoreBinaria();
  17.             if (info < this.raiz.getInfo()) {
  18.                 if (this.raiz.getEsquerda() != null) {
  19.                     arvore.raiz = this.raiz.getEsquerda();
  20.                     arvore.insereElemento(info);
  21.                 }
  22.                 else this.raiz.setEsquerda(folha);
  23.             }
  24.  
  25.             else if (info > this.raiz.getInfo()) {
  26.                 if (this.raiz.getDireita() != null) {
  27.                     arvore.raiz = this.raiz.getDireita();
  28.                     arvore.insereElemento(info);
  29.                 }
  30.                 else this.raiz.setDireita(folha);
  31.             }
  32.         }
  33.     }
  34.  
  35.     public void preOrdem(Node node) {
  36.         if (node != null) {
  37.             System.out.print(node.getInfo() + " ");
  38.             preOrdem(node.getEsquerda());
  39.             preOrdem(node.getDireita());
  40.         }
  41.     }
  42.  
  43.     public void emOrdem(Node node) {
  44.         if (node != null) {
  45.             emOrdem(node.getEsquerda());
  46.             System.out.print(node.getInfo() + " ");
  47.             emOrdem(node.getDireita());
  48.         }
  49.     }
  50.     public void posOrdem(Node node) {
  51.         if (node != null) {
  52.             posOrdem(node.getEsquerda());
  53.             posOrdem(node.getDireita());
  54.             System.out.print(node.getInfo() + " ");
  55.         }
  56.     }
  57.  
  58.     public Node removeMaior() {
  59.         ArvoreBinaria arvore = new ArvoreBinaria();
  60.         if (vazia()) System.out.println("Arvore vazia");
  61.         else if (this.raiz.getDireita() == null) {
  62.             Node removido = this.raiz;
  63.             if (this.raiz.getEsquerda() == null) {
  64.                 this.raiz = null;
  65.             }
  66.             else {
  67.                 this.raiz = this.raiz.getEsquerda();
  68.             }
  69.             return removido;
  70.         }
  71.  
  72.         else {
  73.             arvore.raiz = this.raiz.getDireita();
  74.             arvore.removeMaior();
  75.             this.raiz.setDireita(arvore.raiz);
  76.         }
  77.         return null;
  78.     }
  79.  
  80.     public Node removeMenor() {
  81.         ArvoreBinaria arvore = new ArvoreBinaria();
  82.         if (vazia()) System.out.println("Arvore vazia");
  83.         else if (this.raiz.getEsquerda() == null) {
  84.             Node removido = this.raiz;
  85.             if (this.raiz.getDireita() == null) {
  86.                 this.raiz = null;
  87.             }
  88.             else {
  89.                 this.raiz = this.raiz.getDireita();
  90.             }
  91.             return removido;
  92.         }
  93.  
  94.         else {
  95.             arvore.raiz = this.raiz.getEsquerda();
  96.             arvore.removeMenor();
  97.             this.raiz.setEsquerda(arvore.raiz);
  98.         }
  99.         return null;
  100.     }
  101.  
  102.     public Node remove(int info) {
  103.         ArvoreBinaria arvore = new ArvoreBinaria();
  104.         if (vazia()) System.out.println("Arvore vazia");
  105.         else if (info == this.raiz.getInfo()) {
  106.             Node removido = this.raiz;
  107.             if (this.raiz.getDireita() == null && this.raiz.getEsquerda() == null) {
  108.                 this.raiz = null;
  109.             }
  110.             else if (this.raiz.getEsquerda() == null) {
  111.                 this.raiz = this.raiz.getDireita();
  112.             }
  113.             else if (this.raiz.getDireita() == null) {
  114.                 this.raiz = this.raiz.getEsquerda();
  115.             }
  116.             else {
  117.                 ArvoreBinaria arvore2 = new ArvoreBinaria();
  118.                 arvore2.raiz = this.raiz.getEsquerda();
  119.                 while (arvore2.raiz.getDireita() != null) {
  120.                     arvore2.raiz = arvore2.raiz.getDireita();
  121.                 }
  122.                 this.raiz.setInfo(arvore2.raiz.getInfo());
  123.                 arvore2.raiz = this.raiz.getEsquerda();
  124.                 arvore2.removeMaior();
  125.                 this.raiz.setEsquerda(arvore2.raiz);
  126.  
  127.             }
  128.             return removido;
  129.         }
  130.         else if (info < this.raiz.getInfo()) {
  131.             arvore.raiz = this.raiz.getEsquerda();
  132.             arvore.remove(info);
  133.             this.raiz.setEsquerda(arvore.raiz);
  134.         }
  135.         else if (info > this.raiz.getInfo()) {
  136.             arvore.raiz = this.raiz.getDireita();
  137.             arvore.remove(info);
  138.             this.raiz.setDireita(arvore.raiz);
  139.         }
  140.         return null;
  141.     }
  142.  
  143.  
  144.     public Node getRaiz() {
  145.         return raiz;
  146.     }
  147. }
Add Comment
Please, Sign In to add comment