No scusa sono stato io a linkarti la roba sbagliata. xD
Dovrei avere del codice, ma è forse sull'altro pc... se lo trovo lo incollo qui.
EDIT:
Trovato, era disperso sulla chiavetta!
codice:
// Struttura Nodo
class Nodo {
private int n;
private Nodo left, right;
private String p;
Nodo(int n, Nodo left, Nodo right, String p) {
this.n = n;
this.left = left;
this.right = right;
this.p = p;
}
int getN() {
return n;
}
Nodo getLeft() {
return left;
}
Nodo getRight() {
return right;
}
void setLeft(Nodo l) {
this.left = l;
}
void setRight(Nodo r) {
this.right = r;
}
String getP() {
return p;
}
}
// Alabero binario
class BinaryTree {
private Nodo radice;
// Riceve in ingresso un array di valori
// da inserire nell'albero
BinaryTree(int[] values) {
Nodo nodo = null;
String p = " ";
for(int i=0; i<values.length; i++) {
nodo = insertNodo(nodo, values[i], p);
}
radice = nodo;
}
void showTree() {
show(radice);
}
private void show(Nodo start) {
if(start != null) {
System.out.println(start.getP()+" --> "+start.getN());
show(start.getLeft());
show(start.getRight());
}
}
// Inserisce "val" nel nodo corretto
private Nodo insertNodo(Nodo n, int val,String p) {
// Se null non ha nodi figli; quindi creo
// un nuovo nodo ed inizializzo i figli a null
if(n == null) {
n = new Nodo(val, null, null, p);
}
else {
// Se il valore è maggiore al valore del nodo,
// inserisco il valore nel figlio destro
// (creandolo ricorsivamente)
if(n.getN() < val) {
p += "R";
n.setRight(insertNodo(n.getRight(), val, p));
}
// Altrimenti nel sinistro...
else if(n.getN() > val) {
p += "L";
n.setLeft(insertNodo(n.getLeft(), val, p));
}
}
return n;
}
boolean present(int element) {
return isPresent(element,radice);
}
private boolean isPresent(int element, Nodo radice) {
if(radice != null) {
if(radice.getN() == element) return true;
else if(radice.getN() < element) {
return isPresent(element, radice.getRight());
}
else {
return isPresent(element, radice.getLeft());
}
}
return false;
}
}
class TestTree {
public static void main(String[] args) {
BinaryTree bt = new BinaryTree(new int[] {104,32,121,152,44,200,23, 55, 64, 12, 32, 91, 4});
bt.showTree();
int elemento = 91;
System.out.println("Elemento da cercare: "+elemento+" --> "+bt.present(elemento));
}
}