Sie sind auf Seite 1von 9

BUSQUEDA BINARIA

Diagrama de flujo

Pseudocodigo
1
0
2
0
3
0
4
0
Set first to 0
5 Set last to the last index in the list
0 Set found to false
6 Set position to −1
0 while found is false and first is less than or equal to last
7     Set middle to the index halfway between first and last
    if list[middle] equals the desired value
0         Set found to true
8         Set position to middle
0     else if list[middle] is greater than the desired value
9         Set last to middle − 1
    else
1         Set first to middle + 1
0 return position
1
1
1
2
1
3
1
4

Código
Búsqueda árbol
Diagrama de flujo
Pseudocodigo
inorden(nodo)
mientras tieneHijoIzquierdo(nodo) hacer
nodo = nodo.izquierda
hacer
visita(nodo)
si (tieneHijoDerecho(nodo)) entonces
nodo = nodo.derecha
mientras tieneHijoIzquierdo(nodo) hacer
nodo = nodo.izquierda
de-lo-contrario
mientras nodo.padre ≠ null y nodo == nodo.padre.derecha hacer
nodo = nodo.padre
nodo = nodo.padre
mientras nodo ≠ null

Código

public class abb {


 
private class nodoArbol {
private abb hd;
private abb hi;
private int dato;
 
private void nodoArbol(){
hd = null;
hi = null;
dato = 0;
}
}
 
public nodoArbol raiz;
 
public void abb(){
nodoArbol raiz = new nodoArbol();
}
 
public boolean esVacio(){
return (raiz == null);
}
 
public void insertar(int a){
if (esVacio()) {
nodoArbol nuevo = new nodoArbol();
nuevo.dato = a;
nuevo.hd = new abb();
nuevo.hi = new abb();
raiz = nuevo;
}
else {
if (a > raiz.dato) {
(raiz.hd).insertar(a);
}
if (a < raiz.dato){
(raiz.hi).insertar(a);
}
}
}
 
public void preOrder(){
if (!esVacio()) {
System.out.print( raiz.dato + ", " );
raiz.hi.preOrder();
raiz.hd.preOrder();
}
}
 
public void inOrder(){
if (!esVacio()) {
raiz.hi.inOrder();
System.out.print( raiz.dato + ", " );
raiz.hd.inOrder();
}
}
 
public void posOrder(){
if (!esVacio()) {
raiz.hd.posOrder();
raiz.hi.posOrder();
System.out.print( raiz.dato + ", " );
 
}
}
 
public abb buscar(int a){
abb arbolito = null;
if (!esVacio()) {
if (a == raiz.dato) {
return this;
}
else {
if (a < raiz.dato) {
arbolito = raiz.hi.buscar(a);
}
else {
arbolito = raiz.hd.buscar(a);
}
}
}
return arbolito;
}
 
public boolean existe(int a){
if (!esVacio()) {
if (a == raiz.dato) {
return true;
}
else {
if (a < raiz.dato) {
raiz.hi.existe(a);
}
else {
raiz.hd.existe(a);
}
}
}
return false;
}
 
public int cantidad(){
if (esVacio()) {
return 0;
}
else {
return (1 + raiz.hd.cantidad() + raiz.hi.cantidad());
}
}
 
public int altura() {
if (esVacio()) {
return 0;
}
else {
return (1 + Math.max(((raiz.hi).altura()),
((raiz.hd).altura())));
}
}
 
public int buscarMin() {
abb arbolActual = this;
while( !arbolActual.raiz.hi.esVacio() ) {
arbolActual = arbolActual.raiz.hi;
}
int devuelvo= arbolActual.raiz.dato;
arbolActual.raiz=null;
return devuelvo;
}
 
public int buscarMan() {
abb arbolActual = this;
while( !arbolActual.raiz.hd.esVacio() ) {
arbolActual = arbolActual.raiz.hd;
}
int devuelvo= arbolActual.raiz.dato;
arbolActual.raiz=null;
return devuelvo;
}
 
public boolean esHoja() {
boolean hoja = false;
if( (raiz.hi).esVacio() && (raiz.hd).esVacio() ) {
hoja = true;
}
return hoja;
}
 
public void eliminar(int a) {
abb paraEliminar = buscar(a);
if (!paraEliminar.esVacio()) {
if (paraEliminar.esHoja()) {
paraEliminar.raiz = null;
}
else {
if (!paraEliminar.raiz.hi.esVacio() && !
paraEliminar.raiz.hd.esVacio()) {
paraEliminar.raiz.dato =
paraEliminar.raiz.hd.buscarMin();
}
else {
if (paraEliminar.raiz.hi.esVacio()) {
paraEliminar.raiz = paraEliminar.raiz.hd.raiz;
}else{
paraEliminar.raiz = paraEliminar.raiz.hi.raiz;
}
}
}
}
}
}

Búsqueda secuencial

Diagrama de flujo
Pseudocodigo
Programa: Búsqueda secuencial
Variables
T=10:entero
a[T],temp,i,j,n:entero
x:binario
Inicio
escribir "Llenando arreglo con números aleatorios"
desde i=0 hasta i< T incremento 1 hacer
a[i]=númeroaleatorio
fin desde
escribir "Numero a buscar? "
leer n
x=falso
desde i=0 hasta i< T incremento 1 hacer
si a[i] = n entonces
escribir "Valor encontrado"
escribir "Posición:”, i
x=verdadero
fin si
fin desde
si x=falso entonces
escribir “No se encontró el el número”
fin si
escribir “El arreglo era:"
desde i=0 hasta i< T incremento 1 hacer
escribir a[i]
fin desde
Fin

Código
import java.util.Scanner;

public class main {

public static void main(String[] args) {


// TODO Auto-generated method stub
int [] numeros = {23,43,1,5,2,22,32,78};
Scanner leer = new Scanner(System.in);
boolean existe= false;

System.out.println("ingrese el número a buscar!");


int numBuscado = leer.nextInt();

for(int b = 0; b < numeros.length; b++){

if(numeros[b]==numBuscado){

System.out.println("el numero si existe, en la posicion


"+(b+1));
break;
}
if(b == numeros.length-1){
existe = true;
}
}
if(existe==true){

System.out.println("el numero no existe");


}
}
}

Das könnte Ihnen auch gefallen