Merge fix

This commit is contained in:
Emanuele Slusarz
2026-06-20 20:04:15 +02:00
parent 2a51a8993d
commit 5736e5ec0a
74 changed files with 164 additions and 0 deletions
@@ -700,6 +700,38 @@ public class LinkedBinaryTree<E> implements BinaryTree<E>{
if (diff < -1 || diff > 1) return -1;
return leftValue + rightValue + 1;
// TOTALE 09 LUGLIO 2024
/*
Realizzare un metodo generico statico
public static <E> boolean TwinChildren(BinaryNode<E> root)
che, dato in input l'albero radicato in root, restituisce TRUE se esiste almeno un nodo
che ha esattamente due figli e questi figli sono uguali, FALSE altrimenti.
*/
public static <E> boolean TwinChildren(BinaryNode<E> root) {
// Null Check (SI APPLICA SOLO SUL NODO ROOT)
if (root == null) throw new NullPointerException();
// Se il nodo è un nodo foglia
if (root.getLeft() == null && root.getRight() == null) return false;
// Otteniamo i valori del nodo a sinistra e il nodo a destra
boolean valoreSx = (root.getLeft() != null) ? TwinChildren(root.getLeft()) : false;
// Risaliamo velocemente se il nodo è stato trovato
if (valoreSx) return true;
boolean valoreDx = (root.getRight() != null) ? TwinChildren(root.getRight()) : false;
// Risaliamo velocemente se il nodo è stato trovato
if (valoreDx) return true;
// Controllo quanti nodi ha il nodo corrente
if (root.getLeft() == null || root.getRight() == null) return false;
// Controllo i dati
E leftData = root.getLeft().getData();
E rightData = root.getRight().getData();
if (leftData == null || rightData == null) return false;
return leftData.equals(rightData);
}
}
+20
View File
@@ -12,6 +12,7 @@ import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.ArrayList;
public class Network<Vertex extends Comparable<? super Vertex>> implements Graph<Vertex, Double>{
@@ -103,6 +104,25 @@ public class Network<Vertex extends Comparable<? super Vertex>> implements Graph
Double p = Math.random();
if (cmp < 0) addEdge(s1, s2, -p);
if (cmp > 0) addEdge(s1, s2, p);
Implementare un metodo costruttore della classe Network che prende in input due liste
L1 e L2 di tipo ArrayList<Vertex> e costruisce un nuovo grafo orientato come segue:
gli oggetti presenti nelle due liste rappresentano i vertici; per ogni coppia di vertici
distinti x L1 e y L2, il grafo contiene larco orientato xy, avente come peso un
valore double -p se x<y oppure p se x>y, dove p ε[0,1) è generato con il metodo
Math.random().
*/
public Network(ArrayList<Vertex> L1, ArrayList<Vertex> L2) {
// Null check
if (L1 == null || L2 == null) throw new NullPointerException();
// Aggiunta dei vertici
for (Vertex SS : L1) this.addVertex(SS);
for (Vertex SS : L2) this.addVertex(SS);
// Aggiunta degli archi
for (Vertex x : L1) {
for (Vertex y : L2) {
Double p = Math.random();
if (x.compareTo(y) < 0) p = -p;
this.addEdge(x, y, p);
}
}
}
@@ -0,0 +1,112 @@
package totale.p090724;
import java.util.Set;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.Map;
import java.util.HashMap;
import java.util.TreeMap;
public class AssegnazioniDocenti {
// Mappa di Associazioni <Docente=CF, Scuola=CM>
private Map<String, String> map = new HashMap<String, String>();
private Set<String> docenti = new HashSet<String>();
// Metodo 1
/*
un metodo che aggiunge una nuova associazione o aggiorna la precedente, dati in
input il CF del docente e il CM della scuola (non nulli);
*/
public void insertOrUpdateDocente(
String CF,
String CM
) {
// Null check
if (CF == null || CM == null) throw new NullPointerException();
// Controllo se è vuoto
if (CF.isBlank() || CM.isBlank()) throw new IllegalArgumentException();
// Aggiungo direttamente il record
map.put(CF, CM);
docenti.add(CF);
}
// Metodo 2
/*
un metodo che verifica se un docente è stato assegnato ad una scuola, dato in
input il CF del docente;
*/
public boolean isDocenteAssigned(
String CF
) {
// Null check
if (CF == null) throw new NullPointerException();
// Controllo se è vuoto
if (CF.isBlank()) throw new IllegalArgumentException();
// Verifico la presenza
return map.get(CF) != null;
}
// Metodo 3
/*
un metodo che cancella lassociazione corrente per un docente, se presente, dato
in input il suo CF;
*/
public void removeDocenteReferenceToScuola(
String CF
) {
// Null check
if (CF == null) throw new NullPointerException();
// Controllo se è vuoto
if (CF.isBlank()) throw new IllegalArgumentException();
// Rimozione
map.remove(CF);
}
// Metodo 4
/*
un metodo che, dato in input il CM di una scuola, restituisce linsieme dei codici
fiscali dei docenti assegnati alla data scuola nel corrente anno scolastico, in ordine
lessicografico crescente;
*/
public Set<String> getDocentiPerScuola(
String CM
) {
// Null check
if (CM == null) throw new NullPointerException();
// Controllo se è vuoto
if (CM.isBlank()) throw new IllegalArgumentException();
// Creazione della struttura dati
Set<String> tmp = new TreeSet<String>();
// Selezione dei docenti per il CM fornito
for (String docente : map.keySet()) {
String codiceMeccanografico = map.get(docente);
if (codiceMeccanografico == null || !codiceMeccanografico.equals(CM)) continue;
tmp.add(docente);
}
// Restituisco il Set
return tmp;
}
// Metodo 5
/*
un metodo che restituisce una nuova mappa che associa ad ogni CM
rappresentante una scuola il numero di docenti in servizio presso di essa,
ordinata in base al CM.
*/
public Map<String,Integer> getNumeroDocentiPerScuolaOrdinatoPerCM() {
// Creazione della struttura dati <CM, nDocenti>
Map<String,Integer> tmp = new TreeMap<String,Integer>();
// Trovo le associazioni
for (String docente : map.keySet()) {
String codiceMeccanografico = map.get(docente);
if (codiceMeccanografico == null) continue;
if (tmp.get(codiceMeccanografico) == null) tmp.put(codiceMeccanografico, 1);
else tmp.put(codiceMeccanografico, tmp.get(codiceMeccanografico) + 1);
}
// Return finale
return tmp;
}
}