arch->mac
This commit is contained in:
@@ -14,4 +14,15 @@
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
<filteredResources>
|
||||
<filter>
|
||||
<id>1779562077422</id>
|
||||
<name></name>
|
||||
<type>30</type>
|
||||
<matcher>
|
||||
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||
</matcher>
|
||||
</filter>
|
||||
</filteredResources>
|
||||
</projectDescription>
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
/totale/
|
||||
/binary_tree/
|
||||
/network/
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -574,4 +574,47 @@ public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
else mappa.put(currentData, 1 + mappa.get(currentData));
|
||||
}
|
||||
|
||||
/*
|
||||
TOTALE 2024-02-13
|
||||
Realizzare il metodo statico fatherIsSum(BinaryNode<Integer> root) che, dato l'albero
|
||||
binario radicato nel nodo radice in input, verifica se ogni nodo interno (con almeno un figlio)
|
||||
contiene un intero che è la somma degli interi contenuti nei nodi figli.
|
||||
*/
|
||||
public static boolean fatherIsSum(BinaryNode<Integer> root) {
|
||||
// Null check
|
||||
/*
|
||||
Non verrà richiamato il seguente nodo su un nodo null,
|
||||
tuttavia questo check è fondamentale nella prima chiamata
|
||||
dello stack.
|
||||
*/
|
||||
if (root == null) throw new NullPointerException();
|
||||
|
||||
// Caso nodo foglia
|
||||
/*
|
||||
Nel caso esso sia un nodo foglia la proprietà
|
||||
rimane valida
|
||||
*/
|
||||
if (root.getLeft() == null && root.getRight() == null) return true;
|
||||
|
||||
// Valore di sinistra
|
||||
boolean leftValue = (root.getLeft() == null) ? true : fatherIsSum(root.getLeft());
|
||||
// Se nel valore di sinistra è presente già false, possiamo forzare la risalita dello stack
|
||||
if (!leftValue) return false;
|
||||
|
||||
// Valore di destra
|
||||
boolean rightValue = (root.getRight() == null) ? true : fatherIsSum(root.getRight());
|
||||
// Se nel valore di destra è presente già false, possiamo forzare la risalita dello stack
|
||||
if (!rightValue) return false;
|
||||
|
||||
// Controllo sui dati attuali
|
||||
Integer leftData = (root.getLeft() != null && root.getLeft().getData() != null) ? root.getLeft().getData() : 0;
|
||||
Integer rightData = (root.getRight() != null && root.getRight().getData() != null) ? root.getRight().getData() : 0;
|
||||
|
||||
Integer currentData = (root.getData() != null) ? root.getData() : 0;
|
||||
|
||||
return Integer.compare(currentData, leftData + rightData) == 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
package totale.p240213;
|
||||
|
||||
import java.util.TreeMap;
|
||||
import java.util.HashMap;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class Discendenza {
|
||||
|
||||
/*
|
||||
<CF_Figlio, CF_Padre>
|
||||
*/
|
||||
private TreeMap<String, String> discendenza = new TreeMap<String, String>();
|
||||
|
||||
// Metodo 1
|
||||
public String aggiungiRelazione(String cfFiglio, String cfPadre) {
|
||||
// Null Check
|
||||
if (cfFiglio == null || cfPadre == null) throw new NullPointerException();
|
||||
// CF Check
|
||||
if (cfFiglio.length() != 16 || cfPadre.length() != 16) throw new IllegalArgumentException();
|
||||
// Otteniamo il vecchio valore se presente
|
||||
String vecchioPadre = discendenza.get(cfFiglio);
|
||||
// Aggiungiamo il nuovo valore
|
||||
discendenza.put(cfFiglio, cfPadre);
|
||||
// Restituisco il valore
|
||||
return vecchioPadre;
|
||||
}
|
||||
|
||||
// Metodo 2
|
||||
public String cancellaRelazione(String cfFiglio) {
|
||||
// Null Check
|
||||
if (cfFiglio == null) throw new NullPointerException();
|
||||
// Rimuoviamo il vecchio valore
|
||||
return discendenza.remove(cfFiglio);
|
||||
}
|
||||
|
||||
// Metodo 3
|
||||
public Set<String> getFigli() {
|
||||
return new TreeSet<String>(discendenza.keySet());
|
||||
}
|
||||
|
||||
// Metodo 4
|
||||
public Set<String> getPadri() {
|
||||
// Costruzione della struttura dati.
|
||||
Set<String> padriByMeseNascita = new TreeSet<String>(new Comparator<String>() {
|
||||
@Override
|
||||
public int compare(String s1, String s2) {
|
||||
Character c1 = s1.charAt(8);
|
||||
Character c2 = s2.charAt(8);
|
||||
|
||||
int cmp = Character.compare(c1, c2);
|
||||
if (cmp != 0) return cmp;
|
||||
|
||||
return s1.compareTo(s2);
|
||||
}
|
||||
});
|
||||
// Aggiunta di tutti i padri
|
||||
for (String cfFiglio : discendenza.keySet()) {
|
||||
String cfPadre = discendenza.get(cfFiglio);
|
||||
if (cfPadre == null) continue;
|
||||
padriByMeseNascita.add(cfPadre);
|
||||
}
|
||||
// Restituisco la struttura dati
|
||||
return padriByMeseNascita;
|
||||
}
|
||||
|
||||
// Metodo 5
|
||||
public Set<String> getAscendenti(String cfFiglio) {
|
||||
// Null Check
|
||||
if (cfFiglio == null) throw new NullPointerException();
|
||||
// Se la mappa è vuota
|
||||
if (discendenza.isEmpty()) return null;
|
||||
// Se la mappa non contiene il cfFiglio
|
||||
if (!discendenza.containsKey(cfFiglio)) return null;
|
||||
// E' presente il cfFiglio, possiamo cercare
|
||||
Set<String> ascendenti = new LinkedHashSet<String>();
|
||||
while (true) {
|
||||
String currentPadre = discendenza.get(cfFiglio);
|
||||
if (currentPadre == null) break;
|
||||
// Se è presente un ciclo esci
|
||||
if (ascendenti.contains(currentPadre)) break;
|
||||
ascendenti.add(currentPadre);
|
||||
cfFiglio = currentPadre;
|
||||
}
|
||||
// Ritorno degli ascendenti
|
||||
return ascendenti;
|
||||
}
|
||||
|
||||
// Metodo 6
|
||||
public double averageNumFigli() {
|
||||
// Mappa <Padri, nFigli>
|
||||
HashMap<String, Integer> occorrenzeFigli = new HashMap<String, Integer>();
|
||||
// Conto quanti figli ha ciascun padre
|
||||
for (String cfFiglio : discendenza.keySet()) {
|
||||
String cfPadre = discendenza.get(cfFiglio);
|
||||
if (cfPadre == null) continue;
|
||||
if (occorrenzeFigli.get(cfPadre) == null) occorrenzeFigli.put(cfPadre, 1);
|
||||
else occorrenzeFigli.put(cfPadre, occorrenzeFigli.get(cfPadre) + 1);
|
||||
}
|
||||
// Verifico se non sono presenti figli
|
||||
if (occorrenzeFigli.isEmpty()) return 0;
|
||||
// Calcolo la media
|
||||
Double avg = 0.0;
|
||||
for (String cfPadre : occorrenzeFigli.keySet()) avg += occorrenzeFigli.get(cfPadre);
|
||||
return avg / occorrenzeFigli.size();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user