Labirinti
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
import static java.lang.Math.max;
|
||||
|
||||
class Nodo{
|
||||
static java.util.Random rnd = new java.util.Random();
|
||||
int dato;
|
||||
Nodo left, right;
|
||||
|
||||
public Nodo(int dato, Nodo left, Nodo right) {
|
||||
this.dato = dato;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
static void stampa(Nodo n, int livello, String lr) {
|
||||
if(n==null) return;
|
||||
for(int i=0;i<livello-1;i++)
|
||||
@@ -19,7 +18,6 @@ class Nodo{
|
||||
stampa(n.left, livello+1,"L");
|
||||
stampa(n.right, livello+1,"R");
|
||||
}
|
||||
|
||||
/*
|
||||
0) fissate l'intestazione della funzione e
|
||||
pensate che esista già e che funzioni
|
||||
@@ -28,17 +26,14 @@ class Nodo{
|
||||
2) cosa fare col dato attuale e cosa fare con i rimanenti
|
||||
|
||||
*/
|
||||
|
||||
static int somma(Nodo albero) {
|
||||
if(albero==null) return 0;
|
||||
return albero.dato+somma(albero.left)+somma(albero.right);
|
||||
}
|
||||
|
||||
static int conta(Nodo albero) {//conta quanti sono i nodi
|
||||
if(albero==null) return 0;
|
||||
return 1+conta(albero.left)+conta(albero.right);
|
||||
}
|
||||
|
||||
static int contaFoglie(Nodo albero) {//conta quanti sono le foglie (nodi senza figli)
|
||||
if(albero==null) return 0;
|
||||
if (albero.left == null && albero.right == null) {
|
||||
@@ -47,28 +42,61 @@ class Nodo{
|
||||
return contaFoglie(albero.left) + contaFoglie(albero.right);
|
||||
}
|
||||
}
|
||||
|
||||
static Nodo insOrd(int val, Nodo albero) {
|
||||
static Nodo insOrd(int val, Nodo albero) { //Inserimento ordinato
|
||||
if(albero==null) return new Nodo(val, null, null);
|
||||
if(val>albero.dato) return
|
||||
new Nodo(albero.dato, albero.left, insOrd(val, albero.right));
|
||||
return new Nodo(albero.dato, insOrd(val, albero.left), albero.right);
|
||||
}
|
||||
static boolean trova(Nodo albero, int valore) {
|
||||
static boolean trova(Nodo albero, int valore) { //Trova un valore
|
||||
if(albero==null) return false;
|
||||
else if(albero.dato==valore) return true;
|
||||
else return trova(albero.left, valore) || trova(albero.right, valore);
|
||||
}
|
||||
static String valoriNelLivello(Nodo albero, int livello){
|
||||
static String valoriNelLivello(Nodo albero, int livello){ //Trova un valore in un livello
|
||||
if(albero==null) return "";
|
||||
else if(livello==0) return albero.dato + " ";
|
||||
else return valoriNelLivello(albero.left, livello-1) + valoriNelLivello(albero.right, livello-1);
|
||||
}
|
||||
static int altezza(Nodo albero){
|
||||
static int altezza(Nodo albero){ //Trova l'altezza dell'albero
|
||||
if(albero==null) return 0;
|
||||
else if (albero.left == null && albero.right == null) return 1;
|
||||
return 1 + max(altezza(albero.left), altezza(albero.right));
|
||||
}
|
||||
static boolean uguali(Nodo a, Nodo b) { //Controlla che i due alberi siano uguali
|
||||
if (a == null && b == null) return true;
|
||||
if (a == null || b == null) return false;
|
||||
if (a.dato != b.dato) return false;
|
||||
return uguali(a.left, b.left) && uguali(a.right, b.right);
|
||||
}
|
||||
static int profondita(Nodo albero, int val) { //Restituisce la profondità massima
|
||||
if(albero==null) return -1;
|
||||
else if(albero.left == null && albero.right == null && albero.dato != val) return -1;
|
||||
else if(albero.dato == val) return 1;
|
||||
int pl = profondita(albero.left,val);
|
||||
int pr = profondita(albero.right,val);
|
||||
int m = max(pl, pr);
|
||||
if (m == -1) return -1;
|
||||
return 1 + m;
|
||||
}
|
||||
static int contaFiglio(Nodo albero) { //Conta i nodi che hanno un solo figlio
|
||||
int count = 0;
|
||||
if(albero==null) return count;
|
||||
else if ((albero.left == null && albero.right != null) || (albero.left != null && albero.right == null)) count = 1;
|
||||
return count + contaFiglio(albero.left) + contaFiglio(albero.right);
|
||||
}
|
||||
static int contaNelLivello(Nodo albero, int livello) { //Conta i nodi nel livello
|
||||
if(albero==null) return 0;
|
||||
else if (livello == 0) return 1;
|
||||
return contaNelLivello(albero.left, livello-1) + contaNelLivello(albero.right, livello-1);
|
||||
}
|
||||
static Nodo crea(int altezza) { //Crea un albero con altezza determinata
|
||||
if (altezza == 0) return null;
|
||||
else if (altezza == 1) return new Nodo(rnd.nextInt(41) + 10,null,null);
|
||||
Nodo left = crea(altezza - 1);
|
||||
Nodo right = crea(altezza - 1);
|
||||
return new Nodo(rnd.nextInt(41) + 10, left, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -92,5 +120,4 @@ public class Main {
|
||||
System.out.println("Somma: " + Nodo.somma(n4));
|
||||
System.out.println("Trovato 24? " + Nodo.trova(n4,24));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user