



Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Prepare-se para as provas
Estude fácil! Tem muito documento disponível na Docsity
Prepare-se para as provas com trabalhos de outros alunos como você, aqui na Docsity
Os melhores documentos à venda: Trabalhos de alunos formados
Prepare-se com as videoaulas e exercícios resolvidos criados a partir da grade da sua Universidade
Responda perguntas de provas passadas e avalie sua preparação.
Ganhe pontos para baixar
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Comunidade
Peça ajuda à comunidade e tire suas dúvidas relacionadas ao estudo
Descubra as melhores universidades em seu país de acordo com os usuários da Docsity
Guias grátis
Baixe gratuitamente nossos guias de estudo, métodos para diminuir a ansiedade, dicas de TCC preparadas pelos professores da Docsity
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
1 / 7
Esta página não é visível na pré-visualização
Não perca as partes importantes!
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; }