Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ArvoreBinaria {
- private Node raiz;
- public ArvoreBinaria() {
- this.raiz = null;
- }
- private boolean vazia() {
- return this.raiz == null;
- }
- public void insereElemento(int info) {
- Node folha = new Node(info);
- if (vazia()) this.raiz = folha;
- else {
- ArvoreBinaria arvore = new ArvoreBinaria();
- if (info < this.raiz.getInfo()) {
- if (this.raiz.getEsquerda() != null) {
- arvore.raiz = this.raiz.getEsquerda();
- arvore.insereElemento(info);
- }
- else this.raiz.setEsquerda(folha);
- }
- else if (info > this.raiz.getInfo()) {
- if (this.raiz.getDireita() != null) {
- arvore.raiz = this.raiz.getDireita();
- arvore.insereElemento(info);
- }
- else this.raiz.setDireita(folha);
- }
- }
- }
- public void preOrdem(Node node) {
- if (node != null) {
- System.out.print(node.getInfo() + " ");
- preOrdem(node.getEsquerda());
- preOrdem(node.getDireita());
- }
- }
- public void emOrdem(Node node) {
- if (node != null) {
- emOrdem(node.getEsquerda());
- System.out.print(node.getInfo() + " ");
- emOrdem(node.getDireita());
- }
- }
- public void posOrdem(Node node) {
- if (node != null) {
- posOrdem(node.getEsquerda());
- posOrdem(node.getDireita());
- System.out.print(node.getInfo() + " ");
- }
- }
- public Node removeMaior() {
- ArvoreBinaria arvore = new ArvoreBinaria();
- if (vazia()) System.out.println("Arvore vazia");
- else if (this.raiz.getDireita() == null) {
- Node removido = this.raiz;
- if (this.raiz.getEsquerda() == null) {
- this.raiz = null;
- }
- else {
- this.raiz = this.raiz.getEsquerda();
- }
- return removido;
- }
- else {
- arvore.raiz = this.raiz.getDireita();
- arvore.removeMaior();
- this.raiz.setDireita(arvore.raiz);
- }
- return null;
- }
- public Node removeMenor() {
- ArvoreBinaria arvore = new ArvoreBinaria();
- if (vazia()) System.out.println("Arvore vazia");
- else if (this.raiz.getEsquerda() == null) {
- Node removido = this.raiz;
- if (this.raiz.getDireita() == null) {
- this.raiz = null;
- }
- else {
- this.raiz = this.raiz.getDireita();
- }
- return removido;
- }
- else {
- arvore.raiz = this.raiz.getEsquerda();
- arvore.removeMenor();
- this.raiz.setEsquerda(arvore.raiz);
- }
- return null;
- }
- public Node remove(int info) {
- ArvoreBinaria arvore = new ArvoreBinaria();
- if (vazia()) System.out.println("Arvore vazia");
- else if (info == this.raiz.getInfo()) {
- Node removido = this.raiz;
- if (this.raiz.getDireita() == null && this.raiz.getEsquerda() == null) {
- this.raiz = null;
- }
- else if (this.raiz.getEsquerda() == null) {
- this.raiz = this.raiz.getDireita();
- }
- else if (this.raiz.getDireita() == null) {
- this.raiz = this.raiz.getEsquerda();
- }
- else {
- ArvoreBinaria arvore2 = new ArvoreBinaria();
- arvore2.raiz = this.raiz.getEsquerda();
- while (arvore2.raiz.getDireita() != null) {
- arvore2.raiz = arvore2.raiz.getDireita();
- }
- this.raiz.setInfo(arvore2.raiz.getInfo());
- arvore2.raiz = this.raiz.getEsquerda();
- arvore2.removeMaior();
- this.raiz.setEsquerda(arvore2.raiz);
- }
- return removido;
- }
- else if (info < this.raiz.getInfo()) {
- arvore.raiz = this.raiz.getEsquerda();
- arvore.remove(info);
- this.raiz.setEsquerda(arvore.raiz);
- }
- else if (info > this.raiz.getInfo()) {
- arvore.raiz = this.raiz.getDireita();
- arvore.remove(info);
- this.raiz.setDireita(arvore.raiz);
- }
- return null;
- }
- public Node getRaiz() {
- return raiz;
- }
- }
Add Comment
Please, Sign In to add comment