Docsity
Docsity

Prepare-se para as provas
Prepare-se para as provas

Estude fácil! Tem muito documento disponível na Docsity


Ganhe pontos para baixar
Ganhe pontos para baixar

Ganhe pontos ajudando outros esrudantes ou compre um plano Premium


Guias e Dicas
Guias e Dicas

Árvore Binária de Busca: Implementação e Algoritmos, Resumos de Estruturas de Dados e Algoritmos

Documento contendo a implementação de uma árvore binária de busca em java, com métodos para inserção, remoção, pesquisa, calculo de altura e outros algoritmos relacionados.

Tipologia: Resumos

2021

Compartilhado em 20/07/2022

lourenco-benjamim-uate
lourenco-benjamim-uate 🇲🇿

2 documentos

1 / 7

Toggle sidebar

Esta página não é visível na pré-visualização

Não perca as partes importantes!

bg1
package tad.tree.binaria.busca;
import java.awt.Container;
import java.awt.List;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.LinkedList;
import javax.swing.text.html.HTMLDocument.Iterator;
public class BinarySearchTree {
private Node root;
private int size;
public BinarySearchTree() {
root = null;
size = 0;
}
public Node root(){
return root;
}
public int size(){
return size;
}
public boolean isEmpty(){
return root == null;
}
/*private boolean search(Node v, int e){
if(v == null)
return false;
if(v.getData() == e)
return true;
if(v.getData() > e){
return search(v.getLeft(), e);
}
return search(v.getRight(), e);
}*/
private Node searchE(Node v, int e){
if(v == null)
return null;
if(v.getData() == e)
return v;
if(v.getData() > e){
return searchE(v.getLeft(), e);
}
return searchE(v.getRight(), e);
pf3
pf4
pf5

Pré-visualização parcial do texto

Baixe Árvore Binária de Busca: Implementação e Algoritmos e outras Resumos em PDF para Estruturas de Dados e Algoritmos, somente na Docsity!

package tad.tree.binaria.busca;

import java.awt.Container; import java.awt.List; import java.util.ArrayList; import java.util.Enumeration; import java.util.LinkedList;

import javax.swing.text.html.HTMLDocument.Iterator;

public class BinarySearchTree {

private Node root; private int size;

public BinarySearchTree() { root = null; size = 0; }

public Node root(){ return root; }

public int size(){ return size; }

public boolean isEmpty(){ return root == null; }

/*private boolean search(Node v, int e){ if(v == null) return false;

if(v.getData() == e) return true;

if(v.getData() > e){ return search(v.getLeft(), e); } return search(v.getRight(), e); }*/

private Node searchE(Node v, int e){ if(v == null) return null;

if(v.getData() == e) return v;

if(v.getData() > e){ return searchE(v.getLeft(), e); } return searchE(v.getRight(), e);

public Node parent(Node v){ if(root == null) return null;

return parent(root, v);

private Node parent(Node aux, Node v){ if(aux.equals(v)) return null;

if(aux.getData() > v.getData()){ if(aux.getLeft() != null){ if(aux.getLeft().getData() == v.getData()) return aux; else return parent(aux.getLeft(), v); }

}else{ if(aux.getRight() != null){ if(aux.getRight().getData() == v.getData()) return aux;

return parent(aux.getRight(), v); }

return null;

private Node parent(Node aux, int e){ if(aux == null || aux.getData() == e) return null;

if(isExternal(aux)) return aux; else{ if(aux.getData() > e){ if(aux.getLeft() != null) return parent(aux.getLeft(), e); else return aux; }else{ if(aux.getRight() != null) return parent(aux.getRight(), e); else

if(aux == null) return; if(isExternal(aux)) elems.add(aux.getData()); else{ infixo(aux.getLeft(), elems); elems.add(aux.getData()); infixo(aux.getRight(), elems); } return;

public void replace(Node v, int e){ v.setData(e); }

public boolean insert(int e){

Node newElem = new Node(e);

if(root == null){ root = newElem; size++; return true; }else{ Node aux = parent(root, e);

if(aux == null) return false;

if(aux.getData() > e) aux.setLeft(newElem); else aux.setRight(newElem); size++; return true;

public boolean remove(int e){

if(isEmpty()) // verifica se a árvore é vazia e retorna false caso ela seja. return false;

Node aux = searchE(root, e);

if(aux == null) // verifica se existe na árvore um elemento com a chave procurada e retorna false caso não exista. return false;

if(isExternal(aux)){ // verificamos se o nó a remover é externo. if(isRoot(aux)){ // verifica se o nó a remover

é a raiz. root = null; }else{ Node parent = parent(aux);

if(parent.getLeft().equals(aux)) parent.setLeft(null); else parent.setRight(null); } }else{ if(aux.getLeft() == null || aux.getRight() == null){ // verifica se é um nó com um filho. Node parent = parent(aux); if(isRoot(aux)){ // verifica se o nó a remover é a raiz. if(aux.getLeft() == null) root = aux.getRight(); else root = aux.getLeft(); }else{ if(aux.getLeft() == null){ // verifica se o filho esquerdo é igual a null.

if(parent.getLeft().equals(aux)){ // verifica se o nó a remover é o filho esquerdo do pai.

parent.setLeft(aux.getRight()); }else{

parent.setRight(aux.getRight()); } }else{

if(parent.getLeft().equals(aux)){ // verifica se o nó a remover é o filho esquerdo do pai.

parent.setLeft(aux.getLeft()); }else{

parent.setRight(aux.getLeft()); } } }

}else{ Node descendent = leftDescendent(aux.getRight()); Node descParent = parent(descendent);

if(isRoot(aux)){ // verifica se o nó a ser removido é raiz.

descParent.setLeft(descendent.getRight());

descendent.setRight(aux.getRight());

high(v.getLeft()); if(v.getLeft() != null) v.setHigh(1+v.getLeft().getHigh()); high(v.getRight()); if(v.getRight() != null){ if(v.getHigh() < v.getRight().getHigh()+1)

v.setHigh(1+v.getRight().getHigh()); } } return; }