Buscar

ArvoreB, Insere, Exclui, InOrden Arvore B em Java

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

ArvoreB mod and/build.xml
 
 Builds, tests, and runs the project ArvoreB.
 
 
ArvoreB mod and/build/built-jar.properties
#Sat, 03 Jun 2017 10:35:42 -0300
/home/joao/Documentos/Pasta\ sem\ t\u00edtulo/ArvoreB\ mod\ and=
ArvoreB mod and/build/classes/.netbeans_automatic_build
ArvoreB mod and/build/classes/.netbeans_update_resources
ArvoreB mod and/build/classes/arvoreb/BTree.class
ArvoreB mod and/build/classes/arvoreb/BenditoN.class
ArvoreB mod and/build/classes/arvoreb/No.class
ArvoreB mod and/build/classes/arvoreb/Principal.class
ArvoreB mod and/dist/ArvoreB.jar
META-INF/MANIFEST.MF
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.7
Created-By: 1.8.0_131-b11 (Oracle Corporation)
Class-Path: 
X-COMMENT: Main-Class will be added automatically by build
Main-Class: arvoreb.Principal
arvoreb/BTree.class
arvoreb/BenditoN.class
arvoreb/No.class
arvoreb/Principal.class
ArvoreB mod and/dist/README.TXT
========================
BUILD OUTPUT DESCRIPTION
========================
When you build an Java application project that has a main class, the IDE
automatically copies all of the JAR
files on the projects classpath to your projects dist/lib folder. The IDE
also adds each of the JAR files to the Class-Path element in the application
JAR files manifest file (MANIFEST.MF).
To run the project from the command line, go to the dist folder and
type the following:
java -jar "ArvoreB.jar" 
To distribute this project, zip up the dist folder (including the lib folder)
and distribute the ZIP file.
Notes:
* If two JAR files on the project classpath have the same name, only the first
JAR file is copied to the lib folder.
* Only JAR files are copied to the lib folder.
If the classpath contains other types of files or folders, these files (folders)
are not copied.
* If a library on the projects classpath also has a Class-Path element
specified in the manifest,the content of the Class-Path element has to be on
the projects runtime path.
* To set a main class in a standard Java project, right-click the project node
in the Projects window and choose Properties. Then click Run and enter the
class name in the Main Class field. Alternatively, you can manually type the
class name in the manifest Main-Class element.
ArvoreB mod and/manifest.mf
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
ArvoreB mod and/nbproject/build-impl.xml
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must set src.dir
 Must set test.src.dir
 Must set build.dir
 Must set dist.dir
 Must set build.classes.dir
 Must set dist.javadoc.dir
 Must set build.test.classes.dir
 Must set build.test.results.dir
 Must set build.classes.excludes
 Must set dist.jar
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must set javac.includes
No tests executed.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must set JVM to use for profiling in profiler.info.jvm
 Must set profiler agent JVM arguments in profiler.info.jvmargs.agent
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select some files in the IDE or set javac.includes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 To run this application from the command line without
Ant, try:
 
 java -jar "${dist.jar.resolved}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set run.class
 
 
 
 Must select one file in the IDE or set run.class
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set debug.class
 
 
 
 
 Must select one file in the IDE or set debug.class
 
 
 
 
 Must set fix.includes
 
 
 
 
 
 
 
 
 
 This target only works when run from inside the NetBeans IDE.
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set profile.class
 This target only works when run from inside the NetBeans IDE.
 
 
 
 
 
 
 
 
 This target only works when run from inside the NetBeans IDE.
 
 
 
 
 
 
 
 
 
 
 
 
 This target only works when run from inside the NetBeans IDE.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set run.class
 
 
 
 
 
 Must select some files in the IDE or set test.includes
 
 
 
 
 Must select one file in the IDE or set run.class
 
 
 
 
 Must select one file in the IDE or set applet.url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select some files in the IDE or set javac.includes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Some tests failed; see details above.
 
 
 
 
 
 
 
 
 Must select some files in the IDE or set test.includes
 
 
 
 Some tests failed; see details above.
 
 
 
 Must select some files in the IDE or set test.class
 Must select some method in the IDE or set test.method
 
 
 
 Some tests failed; see details above.
 
 
 
 
 Must select one file in the IDE or set test.class
 
 
 
 Must select one file in the IDE or set test.class
 Must select some method in the IDE or set test.method
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set applet.url
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set applet.url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ArvoreB mod and/nbproject/genfiles.properties
build.xml.data.CRC32=1828ba03
build.xml.script.CRC32=41190f37
build.xml.stylesheet.CRC32=8064a381@1.79.1.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=1828ba03
nbproject/build-impl.xml.script.CRC32=e25045b1
nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48
ArvoreB mod and/nbproject/private/private.properties
compile.on.save=true
user.properties.file=/home/joao/.netbeans/8.2/build.properties
ArvoreB mod and/nbproject/private/private.xml
 
 
 
 
 
ArvoreB mod and/nbproject/project.properties
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processor.options=
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
 ${run.classpath}
debug.test.classpath=\
 ${run.test.classpath}
# Os arquivos em build.classes.dir que devem ser exclu\u00eddos do jar de distribui\u00e7\u00e3o
dist.archive.excludes=
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/ArvoreB.jar
dist.javadoc.dir=${dist.dir}/javadoc
excludes=
includes=**
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.external.vm=true
javac.processorpath=\
 ${javac.classpath}
javac.source=1.8
javac.target=1.8
javac.test.classpath=\
 ${javac.classpath}:\
 ${build.classes.dir}
javac.test.processorpath=\
 ${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
main.class=arvoreb.Principal
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
${javac.classpath}:\
 ${build.classes.dir}
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
run.test.classpath=\
 ${javac.test.classpath}:\
 ${build.test.classes.dir}
source.encoding=UTF-8
src.dir=src
test.src.dir=test
ArvoreB mod and/nbproject/project.xml
 
 org.netbeans.modules.java.j2seproject
 
 
 ArvoreB
 
 
 
 
 
 
 
 
ArvoreB mod and/src/arvoreb/BTree.java
package arvoreb;
import static arvoreb.BenditoN.n;
public class BTree {
 //Atributos
 private No raiz;
 //Construtor
 public BTree() {
 }
 //Getters
 public No getRaiz() {
 return raiz;
 }
 //Métodos
 public void insereArvore(int info, int posArq) {
 No folha, pai;
 int i, pos;
 if (raiz == null) {
 raiz = new No(info, posArq);
 } else {
 folha = navegaAteFolha(info);
 pos = folha.procuraPos(info);
 folha.remaneja(pos);
 folha.setTL(folha.getTL() + 1);
 folha.setvInfo(pos, info);
 folha.setvPos(pos, posArq);
 if (folha.getTL() > 2 * n) {
 pai = localizaPai(folha, info);
 split(folha, pai);
 }
 }
 }
 No navegaAteFolha(int info) {
 int i;
 No p = raiz;
 while (p.getvLig(0) != null) {
 i = 0;
 while (i < p.getTL() && info > p.getvInfo(i)) {
 i = i + 1;
 }
 p = p.getvLig(i);
 }
 return p;
 }
 No localizaPai(No folha, int info) {
 No p, pai;
 int i;
 p = raiz;
 pai = p;
 while (p != folha) {
 i = 0;
 while (i < p.getTL() && info > p.getvInfo(i)) {
 i = i + 1;
 }
 pai = p;
 p = p.getvLig(i);
 }
 return pai;
 }
 public void split(No folha, No pai) {
 No caixa1, caixa2;
 int i = 0, pos = 0;
 int info;
 caixa1 = new No();
 caixa2 = new No();
 for (i = 0; i < n; i++) {
 caixa1.setvInfo(i, folha.getvInfo(i));
 caixa1.setvPos(i, folha.getvPos(i));
 caixa1.setvLig(i, folha.getvLig(i));
 }
 caixa1.setvLig(n, folha.getvLig(n));
 caixa1.setTL(n);
 for (i = n + 1; i < 2 * n + 1; i++) {
 caixa2.setvInfo(i - (n + 1), folha.getvInfo(i));
 caixa2.setvPos(i - (n + 1), folha.getvPos(i));
 caixa2.setvLig(i - (n + 1), folha.getvLig(i));
 }
 caixa2.setvLig(n, folha.getvLig(2 * n + 1));
 caixa2.setTL(n);
 if (pai == folha) {
 folha.setvInfo(0, folha.getvInfo(n));
 folha.setvPos(0, folha.getvPos(n));
 folha.setvLig(0, caixa1);
 folha.setvLig(1, caixa2);
 folha.setTL(1);
 } else {
 info = folha.getvInfo(n);
 pos = pai.procuraPos(info);
 pai.remaneja(pos);
 pai.setTL(pai.getTL() + 1);
 pai.setvInfo(pos, folha.getvInfo(n));
 pai.setvPos(pos, folha.getvPos(n));
 pai.setvLig(pos, caixa1);
 pai.setvLig(pos + 1, caixa2);
 if (pai.getTL() > 2 * n) {
 folha = pai;
 info = folha.getvInfo(n);
 pai = localizaPai(pai, info);
 split(folha, pai);
 }
 }
 }
 public void inOrdem(No raiz) {
 int i = 0;
 if (raiz != null) {
 for (i = 0; i < raiz.getTL(); i++) {
 inOrdem(raiz.getvLig(i));
 System.out.printf("%d, ", raiz.getvInfo(i));
 }
 inOrdem(raiz.getvLig(raiz.getTL()));
 }
 }
 public void exibeInOrdem() {
 No p = null;
 if (raiz == null) {
 System.out.println("A árvore está vazia!");
 } else {
 p = raiz;
 System.out.println("Elementos:");
 inOrdem(p);
 }
 }
 // Começa Remover
 /* Verifica se um elemento existe na Arvore
 * public boolean isIn(int)
 * @param info
 * retorna true se elemento está presente na arvore
 */
 public boolean isIn(int info) {
 No p = raiz;
 int i = 0;
 boolean flag = false;
 for (int pos = 0; pos < p.getTL(); pos++) {
 if (info == p.getvInfo(pos)) {
 flag = true;
 }
 }
 if (!flag) {
 p = navegaAteFolha(info);
 while (i < p.getTL()) {
 if (info == p.getvInfo(i)) {
 flag = true;
 }
 i++;
 }
 }
 return flag;
 }
 /* Exclui elemento de uma folha e remaneja informações e ligações
 * void excluiElementoFolha(No , int)
 * @param folha 
 * @param info
 * return void
 */
 public void excluiElementoFolha(No folha, int info) {
 int i = 0;
 while (info != folha.getvInfo(i)) {
 i++;
 }
 //Remaneja posição do elemento e sua ligação
 while (i < folha.getTL() - 1) {
 folha.setvInfo(i, folha.getvInfo(i + 1));
 folha.setvPos(i, folha.getvPos(i + 1));
 i++;
 }
 folha.setTL(folha.getTL() - 1);
 }
 /* empresta do pai e o pai da folha a direita
 * boolean getDir(int, int, No, No){
 * @param folha 
 * @param info
 * @param pos
 * @param pai 
 * return true se emprestimo for realizado
 */
 public boolean getDir(int info, int pos, No folha, No pai) {
 No aux = pai.getvLig(pos + 1);
 int infoPai, posPai, auxinfo, auxpos;
 // Se a ligação não for nula e TL dir > N
 if (pai.getvLig(pos + 1) != null && pai.getvLig(pos + 1).getTL() > n) {
 auxinfo = aux.getvInfo(0);
 auxpos = aux.getvPos(0);
 infoPai = pai.getvInfo(pos);
 posPai = pai.getvPos(pos);
 remover(aux.getvInfo(0)); 
 pai.setvInfo(pos, auxinfo);
 pai.setvPos(pos, auxpos);
 insereArvore(infoPai, posPai);
 return true;
 }
 return false;
 }
 /* empresta do pai e o pai da folha a direita
 * boolean getEsq(int, int, No, No){
 * @param folha 
 * @param info
 * @param pos
 * @param pai 
 * return true se emprestimo for realizado
 */
 public boolean getEsq(int info, int pos, No folha, No pai) {
 No aux = pai.getvLig(pos);
 int infoPai, posPai, auxinfo, auxpos;
 if (pai.getvLig(pos) != null && pai.getvLig(pos).getTL() > n) {
 auxinfo = aux.getvInfo(aux.getTL() - 1);
 auxpos = aux.getvPos(aux.getTL() - 1);
 infoPai = pai.getvInfo(pos);
 posPai = pai.getvPos(pos);
 remover(aux.getvInfo(aux.getTL() - 1));
 pai.setvInfo(pos, auxinfo);
 pai.setvPos(pos, auxpos);
 insereArvore(infoPai, posPai);
 return true;
 } else {
 return false;
 }
 }
 public void fusao(int pos, No folha, No pai) {
 No aux = pai.getvLig(pos + 1);
 int infoPai, posPai;
 infoPai = pai.getvInfo(pos);
 posPai = pai.getvPos(pos);
 excluiElementoFolha(pai, infoPai);
 for (int p = pos + 1; p <= pai.getTL(); p++) {
 pai.setvLig(p, pai.getvLig(p + 1));
 }
 insereArvore(infoPai,
posPai);
 for (int p = 0; p < aux.getTL(); p++) {
 insereArvore(aux.getvInfo(p), aux.getvPos(p));
 }
 if (pai.getTL() < n && pai != raiz){ // isIn se o TL < N e !=raiz
 fusaoNoPaiSolo(pai);
 }
 }
 //ordem quebrada
 public void fusaoNoPaiSolo(No folha){
 int i = 0, j = 0, pos = 0;
 No aux , p, avo ;
 p = localizaPai(folha, folha.getvInfo(0));
 i = pos(folha.getvInfo(0), p);
 if (p == raiz) {
 if (p.getTL() > 1) {
 if (i == 0) {
 fusao(i, folha, p);
 } else {
 fusao(i - 1, folha, p);
 }
 } else {
 aux = mergePai(p);
 raiz = aux;
 }
 }
 }
 public No mergePai(No pai){
 No esq = pai.getvLig(0);
 No dir = pai.getvLig(1);
 int pos = 0;
 esq.setvInfo(esq.getTL(), pai.getvInfo(0));
 esq.setvPos(esq.getTL(), pai.getvPos(0)); // foi mudado
 esq.setTL(esq.getTL() + 1);
 for (pos = 0; pos < dir.getTL(); pos++) {
 esq.setvLig(esq.getTL(), dir.getvLig(pos));
 esq.setvInfo(esq.getTL(), dir.getvInfo(pos));
 esq.setvPos(esq.getTL(), dir.getvPos(pos));
 esq.setTL(esq.getTL() + 1);
 }
 esq.setvLig(esq.getTL(), dir.getvLig(pos));
 return esq;
 }
 public No killPai(No folha){
 No esq = folha.getvLig(0); 
 No dir = killFilho(folha.getvLig(1), esq);
 int pos = 0;
 for (pos = 0; pos < dir.getTL(); pos++) {
 esq.setvLig(esq.getTL(), dir.getvLig(pos));
 esq.setvInfo(esq.getTL(), dir.getvInfo(pos));
 esq.setvPos(esq.getTL(), dir.getvPos(pos));
 esq.setTL(esq.getTL() + 1);
 }
 esq.setvLig(esq.getTL(), dir.getvLig(pos));
 return esq;
 }
 public No mergeFilhos(No folha){ //Somente se raiz possuir um único elemento
 No esq = folha.getvLig(0); 
 No dir = folha.getvLig(1);
 int pos = 0;
 for (pos = 0; pos < dir.getTL(); pos++) {
 esq.setvLig(esq.getTL(), dir.getvLig(pos));
 esq.setvInfo(esq.getTL(), dir.getvInfo(pos));
 esq.setvPos(esq.getTL(), dir.getvPos(pos));
 esq.setTL(esq.getTL() + 1);
 }
 esq.setvLig(esq.getTL(), dir.getvLig(pos));
 return esq;
 }
 public No killFilho(No folha, No viz) {
 No esq = folha.getvLig(0); 
 No dir = folha.getvLig(1); 
 int pos = 0, auxinfo = esq.getvInfo(0), auxpos = esq.getvPos(0); 
 for (pos = 0; pos < esq.getTL() - 1; pos++) {
 esq.setvInfo(pos, esq.getvInfo(pos + 1)); 
 esq.setvPos(pos, esq.getvPos(pos + 1)); 
 }
 esq.setvInfo(pos, folha.getvInfo(0));
 esq.setvPos(pos, folha.getvPos(0));
 for (pos = 0; pos < dir.getTL(); pos++) {
 esq.setvInfo(esq.getTL(), dir.getvInfo(pos));
 esq.setvPos(esq.getTL(), dir.getvPos(pos));
 esq.setTL(esq.getTL() + 1);
 }
 folha.setvLig(1, esq);
 folha.setvLig(0, viz.getvLig(viz.getTL()));
 folha.setvInfo(0, auxinfo);
 folha.setvPos(0, auxpos);
 return folha;
 }
 public int pos(int info, No pai) {
 int i = 0;
 while (info > pai.getvInfo(i) && i < pai.getTL()) {
 i++;
 }
 return i;
 }
 public boolean apagarNo(No folha, int info) {
 No esq, dir;
 int pos = 0, k = 0;
 //Localiza elemento
 pos = pos(info, folha);
 //filho direita
 esq = folha.getvLig(pos);
 //filho esq
 dir = folha.getvLig(pos + 1);
 //Merge dos filhos no No
 for (k = 0; k < dir.getTL(); k++) {
 esq.setvLig(esq.getTL(), dir.getvLig(k));
 esq.setvInfo(esq.getTL(), dir.getvInfo(k));
 esq.setvPos(esq.getTL(), dir.getvPos(k));
 esq.setTL(esq.getTL() + 1);
 }
 esq.setvLig(esq.getTL(), dir.getvLig(k)); 
 folha.setvLig(pos, esq);
 //remanejando informações e ligações
 for (k = pos; k < folha.getTL(); k++){
 folha.setvInfo(k, folha.getvInfo(k + 1)); 
 folha.setvPos(k, folha.getvPos(k + 1)); 
 if (k != pos) {
 folha.setvLig(k, folha.getvLig(k + 1)); 
 }
 }
 folha.setTL(folha.getTL() - 1);
 return true;
 }
 public No busca(int info) {
 No p = raiz;
 int i = 0;
 boolean flag = false;
 while (!flag) {
 for (i = 0; i < p.getTL(); i++) {
 if (info == p.getvInfo(i)) {
 flag = true;
 }
 }
 if (!flag) {
 int pos = pos(info, p);
 p = p.getvLig(pos);
 }
 }
 return p;
 }
 public void remover(int info) {
 No folha, pai,nAux;
 int lado = 0, pos = 0, aux = 0;
 boolean flag = false;
 if (raiz == null){
 System.out.println("Arvore Vazia!");
 } else {
 if (isIn(info)){
 folha = busca(info);
 if (folha.getvLig(0) == null){//É uma folha?
 if (folha == raiz){ //Esse caralho de folha é raiz?
 excluiElementoFolha(folha, info); //Exclui elemento da folha
 flag = true; 
 } else {//Folha não é a raiz
 pai = localizaPai(folha, info); 
 excluiElementoFolha(folha, info); 
 if (folha.getTL() < n){// N>tl/ empresta Tem elementos a emprestar?
 lado = pos(info, pai);
 //Lado ==0 dir senão esquerda
 if (lado == 0){//Verifica posição atual
 flag = getDir(info, lado, folha, pai);
 } else{
 if (lado > 0 && lado < pai.getTL()){//ta no meio
 flag = getEsq(info, lado - 1, folha, pai);
 if (!flag){
 flag = getDir(info, lado, folha, pai);
 }
 } else{//so pode pegar da esquereda
 flag = getEsq(info, lado - 1, folha, pai);
 }
 }
 if (!flag){//nao pegou de ninguem, verifica lado novamente.
 if (lado == 0) {
 fusao(lado, folha, pai); // Fode todo mundo
 flag = true;
 } else {//
 fusao(lado - 1, folha, pai);
 flag = true;
 }
 }
 }
 }
 if (flag) {
 System.out.println("Excluido.");
 } else {
 System.out.println("Falha na exclusão.");
 }
 } else{// não é um caralho de folha 
 //Se a chave não estiver numa folha, troque-a com seu sucessor imediato.
 flag = emprestaFolha(folha, info);//Tenta pegar de uma folha
 if (!flag){
 if (folha != raiz){// se não estiver na raiz
 pai = localizaPai(folha, info);
 excluiElementoFolha(folha, info);
 lado = pos(info, pai);// Verifica lado
 fusaoNoPaiSolo(pai);
flag = true;
 } else{// se info estiver na raiz
 // A folha tem uma chave a menos que o mínimo. Verifique as páginas irmãs da esquerda e direita
 if (folha.getTL() == n - 1) {
 excluiElementoFolha(folha, info); // 2. Elimine a chave da folha.
 nAux = mergeFilhos(folha);
 raiz = nAux;
 flag = true;
 } else {
 flag = apagarNo(folha, info);
 }
 }
 }
 if (flag) {
 System.out.println("Removido.");
 } else {
 System.out.println("Falha.");
 }
 }
 } else{
 System.out.println("O elemento não existe.");
 }
 }
 }
 
 public boolean emprestaFolha(No folha, int info) {
 No aux = folha;
 int auxinfo = 0, auxpos = 0;
 int pos = pos(info, folha);
 aux = aux.getvLig(pos);
 while (aux.getvLig(aux.getTL()) != null) // Anda até a folha
 {
 aux = aux.getvLig(aux.getTL());
 }
 if (aux.getTL() > n) // Valida para saber se pode emprestar ! caso sim 
 { // Apaga da folha e joga para a raiz 
 auxinfo = aux.getvInfo(aux.getTL() - 1);
 auxpos = aux.getvPos(aux.getTL() - 1);
 excluiElementoFolha(aux, aux.getvInfo(aux.getTL() - 1));
 folha.setvInfo(pos, auxinfo);
 folha.setvPos(pos, auxpos);
 return true;
 }
 aux = folha;
 aux = aux.getvLig(pos + 1);
 while (aux.getvLig(0) != null) // Anda até a folha novamente
 {
 aux = aux.getvLig(0);
 }
 if (aux.getTL() > n) // Valida para saber se pode emprestar ! caso sim Apaga a folha e joga para a raiz
 {
 auxinfo = aux.getvInfo(0);
 auxpos = aux.getvPos(0);
 excluiElementoFolha(aux, aux.getvInfo(0));
 folha.setvInfo(pos, auxinfo);
 folha.setvPos(pos, auxpos);
 return true;
 }
 return false;
 }
}
ArvoreB mod and/src/arvoreb/BenditoN.java
package arvoreb;
public interface BenditoN 
{
 final int n = 5;
}
ArvoreB mod and/src/arvoreb/No.java
package arvoreb;
public class No implements BenditoN
{
 int vInfo[];
 int vPos[];
 No vLig[];
 int TL;
 public No() 
 {
 vInfo = new int[2*n+1];
 vPos = new int [2*n+1];
 vLig = new No [2*n+2];
 for (int i = 0 ; i < 2*n+2 ; i++)
 vLig[i] = null; 
 TL = 0; 
 }
 
 public No(int info, int posArq)
 { 
 this();
 vInfo[0] = info;
 vPos [0] = posArq;
 TL = 1;
 }
 public int getvInfo(int p) 
 {
 return vInfo[p];
 }
 public void setvInfo(int p, int info) 
 {
 vInfo[p] = info;
 }
 public int getvPos(int p) 
 {
 return vPos[p];
 }
 public void setvPos(int p, int pos) 
 {
 vPos[p] = pos;
 }
 public No getvLig (int p) 
 {
 return vLig[p];
 }
 public void setvLig(int p, No lig) 
 {
 vLig[p] = lig;
 }
 public int getTL() 
 {
 return TL;
 }
 public void setTL(int TL) 
 {
 this.TL = TL;
 }
 
 int procuraPos (int info)
 {
 int i=0;
 while (i<TL && info > vInfo[i] )
 {
 i++;
 }
 return i; 
 }
 
 public void remaneja (int pos)
 {
 int i;
 
 vLig[TL+1] = vLig [TL];
 for(i = TL ; i > pos ; i-- )
 {
 vInfo[i] = vInfo[i-1];
 vPos[i] = vPos[i-1];
 vLig[i] = vLig[i-1]; 
 }
 }
 
 
}
ArvoreB mod and/src/arvoreb/Principal.java
package arvoreb;
import java.util.Scanner;
public class Principal 
{
 public static void main(String[] args) 
 {
 Scanner sc = new Scanner (System.in);
 int num;
 BTree b = new BTree();
 
 System.out.println("## Menu ##\n");
 System.out.println("\n[1]Inserir Elementos");
 System.out.println("\n[2]Excluir Elementos");
 System.out.println("\n[3]Exibir In Ordem");
 System.out.println("\n[0]Sair");
 
 num = sc.nextInt();
 
 while(num!=0)
 {
 switch(num)
 {
 case 1:
 while(num!=0)
 {
 System.out.println("Inserir Elemento: ");
 num = sc.nextInt();
 if(num!=0)
 b.insereArvore(num, num);
 }
 
 
 break;
 case 2:
 while(num!=0)
 {
 System.out.println("\nExcluir Elemento: ");
 num = sc.nextInt();
 if(num!=0)
 b.remover(num);
 }
 break;
 case 3:
 No p = b.getRaiz();
 b.exibeInOrdem();
 break;
 case 0:
 num=0;
 break;
 }
 System.out.println("\n## Menu ##");
 System.out.println("\n[1]Inserir Elementos");
 System.out.println("\n[2]Excluir Elementos");
 System.out.println("\n[3]Exibir In Ordem");
 System.out.println("\n[0]Sair\n");
 num = sc.nextInt();
 }
 
 
 
 
 /*
 System.out.println("Inserir Elemento: ");
 num = sc.nextInt();
 while(num!=0)
 {
 b.insereArvore(num, num);
 System.out.println("Inserir Elemento: (0-Sair)");
 num = sc.nextInt();
 }
 
 b.exibeInOrdem();
 
 System.out.println("\nExcluir Elemento: ");
 num = sc.nextInt();
 while(num!=0)
 {
 b.remover(num);
 No p = b.getRaiz();
 b.exibeInOrdem();
 System.out.println("\nExcluir Elemento: (0 - Sair) ");
 num = sc.nextInt();
 }
 
 */
 }
 
}

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Continue navegando