This commit is contained in:
2025-11-20 09:29:02 +01:00
parent ce29e454cc
commit 16f1d7af9e
7 changed files with 153 additions and 0 deletions

30
Alberi1/.gitignore vendored Normal file
View File

@@ -0,0 +1,30 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
.kotlin
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
Alberi1/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

6
Alberi1/.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
Alberi1/.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Alberi1.iml" filepath="$PROJECT_DIR$/Alberi1.iml" />
</modules>
</component>
</project>

6
Alberi1/.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

11
Alberi1/Alberi1.iml Normal file
View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

84
Alberi1/src/NodoMain.java Normal file
View File

@@ -0,0 +1,84 @@
class Nodo{
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++)
System.out.print("\t");
if(livello!=0) System.out.print("+------->");
System.out.println(n.dato+lr);
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
1) caso base, trovare un caso in cui la risposta
è ovvia e non serve computazione
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) {
return 1;
} else {
return contaFoglie(albero.left) + contaFoglie(albero.right);
}
}
static Nodo insOrd(int val, Nodo albero) {
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) {
if(albero==null) return false;
else if(albero.dato==valore) return true;
else return trova(albero.left, valore) || trova(albero.right, valore);
}
}
public class NodoMain {
public static void main(String[] args) {
Nodo n1 = new Nodo(10, null, null);
Nodo n2 = new Nodo(5, null, null);
Nodo n3 = new Nodo(30, null, null);
n1.left = n2;
n1.right = n3;
// Nodo.stampa(n1,0);
Nodo n4 = Nodo.insOrd(23, n1);
n4 = Nodo.insOrd(1, n4);
Nodo.stampa(n4,0,"");
System.out.println("Nodi: " + Nodo.conta(n4));
System.out.println("Foglie: " + Nodo.contaFoglie(n4));
System.out.println("Somma: " + Nodo.somma(n4));
System.out.println("Trovato 24? " + Nodo.trova(n4,24));
}
}