Buscar

Linguagem Java - Arvore B - BTree (Insere e Passeio InOrden) na Arvore Btree

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

ArvoreB/build.xml
 
 Builds, tests, and runs the project ArvoreB.
 
 
ArvoreB/build/built-jar.properties
#Sat, 07 May 2016 11:29:14 -0300
/home/joao/NetBeansProjects/ArvoreB=
ArvoreB/build/classes/.netbeans_automatic_build
ArvoreB/build/classes/.netbeans_update_resources
ArvoreB/build/classes/arvoreb/ArvoreB.class
ArvoreB/build/classes/arvoreb/No.class
ArvoreB/build/classes/arvoreb/OrdenN.class
ArvoreB/build/classes/arvoreb/aplicacao.class
ArvoreB/dist/ArvoreB.jar
META-INF/MANIFEST.MF
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.8.0_77-b03 (Oracle Corporation)
Class-Path: 
X-COMMENT: Main-Class will be added automatically by build
Main-Class: arvoreb.aplicacao
arvoreb/ArvoreB.class
arvoreb/No.class
arvoreb/OrdenN.class
arvoreb/aplicacao.class
ArvoreB/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/manifest.mf
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
ArvoreB/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/nbproject/genfiles.properties
build.xml.data.CRC32=1828ba03
build.xml.script.CRC32=41190f37
build.xml.stylesheet.CRC32=8064a381@1.75.2.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=6fd98f92
nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.75.2.48
ArvoreB/nbproject/private/private.properties
compile.on.save=true
user.properties.file=/home/joao/.netbeans/8.0.2/build.properties
ArvoreB/nbproject/private/private.xml
 
 
 
 
 
ArvoreB/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.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.aplicacao
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/nbproject/project.xml
 
 org.netbeans.modules.java.j2seproject
 
 
 ArvoreB
 
 
 
 
 
 
 
 
ArvoreB/src/arvoreb/ArvoreB.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package arvoreb;
/**
 *
 * @author joao
 */
public class ArvoreB implements OrdenN {
 private No raiz;
 public ArvoreB() {
 this.raiz = null;
 }
 public No getraiz() {
 return this.raiz;
 }
//------------------------------------------------------------------------------
 public No navegaAteFolha(int info) {
 int i;
 No p = raiz;
 while (p.getLig(0) != null) {
 i = 0;
 while (i < p.getTl() && info > p.getInf(i)) {
 i++;
 }
 p = p.getLig(i);
 }
 return p;
 }
 //------------------------------------------------------------------------------
 public int procuraPosicao(No no, int info) {
 int i = 0;
 while (i < no.getTl() && info > no.getInf(i)) {
 i++;
 }
 return i;
 }
//--------------------------------------------------------------------------
 public void remaneja(No no, int pos) {
 int i;
 no.setLig(no.getTl() + 1, no.getLig(no.getTl()));
 for (i = no.getTl(); i > pos; i--) {
 no.setInfo(i, no.getInf(i - 1));
 no.setPosArq(i, no.getPosArq(i) - 1);
 no.setLig(i, no.getLig(i - 1));
 }
 }
//--------------------------------------------------------------------------
 public No localizaPai(No folha, int info) {
 No p = raiz, pai = p;
 int i;
 while (p != folha) {
 i = 0;
 while (i < p.getTl() && info > p.getInf(i)) {
 i++;
 }
 pai = p;
 p = p.getLig(i);
 }
 return pai;
 }
 //--------------------------------------------------------------------------
 public void split(No folha, No pai, int info) {
 No cx1 = new No(), cx2 = new No();
 int i, pos, auxInfo;
 for (i = 0; i < N; i++) {
 cx1.setInfo(i, folha.getInf(i));
 cx1.setPosArq(i, folha.getPosArq(i));
 cx1.setLig(i, folha.getLig(i));
 }
 cx1.setLig(N, folha.getLig(N));
 cx1.setTl(N);
 for (i = N + 1; i < 2 * N + 1; i++) {
 cx2.setInfo(i - (N + 1), folha.getInf(i));
 cx2.setPosArq(i - (N + 1), folha.getPosArq(i));
 cx2.setLig(i - (N + 1), folha.getLig(i));
 }
 cx2.setLig(N, folha.getLig(i));
 cx2.setTl(N);
 if (pai == folha) {
 folha.setInfo(0, folha.getInf(N));
 folha.setPosArq(0, folha.getPosArq(N));
 folha.setLig(0, cx1);
 folha.setLig(1, cx2);
 folha.setTl(1);
 } else {
 auxInfo = folha.getInf(N);
 pos = procuraPosicao(pai, info);
 remaneja(pai, info);
 pai.setTl(pai.getTl() + 1);
 pai.setInfo(pos, folha.getInf(N));
 pai.setPosArq(pos, folha.getPosArq(N));
 pai.setLig(pos, cx1);
 pai.setLig(pos + 1, cx2);
 if (pai.getTl() > 2 * N) {
 folha = pai;
 auxInfo = folha.getInf(N);
 pai = localizaPai(pai, info);
 split(folha, pai, info);
 }
 }
 }
 //--------------------------------------------------------------------------
 public void insere(int info, int posArq) {
 No folha;
 No pai;
 int pos;
 if (raiz == null) {
 raiz = new No(info, posArq);
 } else {
 folha = navegaAteFolha(info);
 pos = procuraPosicao(folha, info);
 remaneja(folha, pos);
 folha.setTl(folha.getTl() + 1);
 folha.setInfo(pos, info);
 folha.setPosArq(pos, posArq);
 if (folha.getTl() > 2 * N) {
 pai = localizaPai(folha, info);
 split(folha, pai, info);
 }
 }
 }
 //--------------------------------------------------------------------------
 public void inOrden(No raiz) {
 if (raiz != null) {
 for (int i = 0; i < raiz.getTl(); i++) {
 inOrden(raiz.getLig(i));
 System.out.println(raiz.getInf(i));
 }
 inOrden(raiz.getLig(raiz.getTl()));
 }
 }
 //--------------------------------------------------------------------------
 public boolean isLeaf(No no) {
 return (no.getLig(0) == null);
 }
 //--------------------------------------------------------------------------
 public No busca(int info) {
 No no = raiz;
 int i;
 boolean flag = false;
 while (no != null && !flag) {
 i = 0;
 while (i < no.getTl() && info > no.getInf(i)) {
 i++;
 }
 if (i < no.getTl() && info == no.getInf(i)) {
 flag = true;
 } else {
 no = no.getLig(i);
 }
 }
 return no;
 }
 public int buscaSucessor(int info) {
 No p=raiz,pAnt;
 int sucessor = 0;
 while(p!=null){
 
 }
 return sucessor;
 }
}
ArvoreB/src/arvoreb/No.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package arvoreb;
/**
 *
 * @author joao
 */
public class No implements OrdenN{
 private int vInfo[];
 private int vPos[];
 private No vLig[];
 private int tl;
 public No() {
 this.vInfo =new int[2*N+1];
 this.vPos = new int[2*N+1];
 this.vLig = new No[2*N+2];
 this.tl=0;
 for(int i=0;i<2*N+2;i++){
 vLig[i]=null;
 }
 }
 public No(int info,int posArq) {
 this.vInfo =new int[2*N+1];
 this.vPos = new int[2*N+1];
 this.vLig = new No[2*N+2];
 this.tl=0;
 for(int i=0;i<2*N+2;i++){
 vLig[i]=null;
 }
 vInfo[0]=info;
 vPos[0]=posArq;
 this.tl=1;
 }
 public void setInfo(int pos,int info){
 vInfo[pos]=info;
 }
 public int getInf(int pos){
 return vInfo[pos];
 }
 public void setLig(int pos,No lig){
 vLig[pos]=lig;
 }
 public No getLig(int pos){
 return vLig[pos];
 }
 public int getPosArq(int pos){
 return vPos[pos];
 }
 public void setPosArq(int pos,int posArq){
 vPos[pos]=posArq;
 }
 public int getTl(){
 return this.tl;
 }
 public void setTl(int tl){
 this.tl=tl;
 }
 
}
ArvoreB/src/arvoreb/OrdenN.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package arvoreb;
/**
 *
 * @author joao
 */
public interface OrdenN {
 public static int N=2;
}
ArvoreB/src/arvoreb/aplicacao.java
/*
 * To change
this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package arvoreb;
/**
 *
 * @author joao
 */
public class aplicacao {
 /**
 * @param args the command line arguments
 */
 public static void main(String[] args) {
 // TODO code application logic here
 ArvoreB arvore = new ArvoreB();
 No aux;
 int teste;
 for(int i=0;i<10;i++){
 System.out.println("Inserindo["+i+"]");
 arvore.insere(i, i);
 }
 arvore.inOrden(arvore.getraiz());
 aux=arvore.busca(8);
 teste=arvore.procuraPosicao(aux, 8);
 System.out.println("Buscou essa buceta:["+aux.getInf(teste)+"]");
 }
 
}

Teste o Premium para desbloquear

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

Continue navegando

Outros materiais