This commit is contained in:
Emanuele Slusarz
2026-05-23 20:37:48 +02:00
parent 30b66dd99b
commit 66f053aba3
25 changed files with 1832 additions and 481 deletions
@@ -1,9 +1,11 @@
package binary_tree;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Stack;
@@ -517,4 +519,59 @@ public class LinkedBinaryTree<E> implements BinaryTree<E>{
}
}
/*
* TOTALE 2024-01-16
* Realizzare un metodo ricorsivo interno alla classe LinkedBinaryTree
* che dato l'albero binario corrente restituisca la distanza della foglia
* più vicina alla radice.
*/
public int shortestLeaf() {
if (root == null) throw new NullPointerException();
return shortestLeaf(root);
}
protected int shortestLeaf(BinaryNode<E> node) {
// Se questa è una foglia
if (node.getLeft() == null && node.getRight() == null) return 1;
// Recuperiamo l'altezza di sinistra e di destra
/*
* Nota BENE: Non si arriverà mai nella situazione in cui vi sono 2 MAX_VALUE assegnati al
* nodo corrente poichè il caso foglia è gestito sopra.
*/
int altezzaMinimaSinistra = (node.getLeft() != null) ? shortestLeaf(node.getLeft()) : Integer.MAX_VALUE;
int altezzaMinimaDestra = (node.getRight() != null) ? shortestLeaf(node.getRight()) : Integer.MAX_VALUE;
// Ritorno del valore foglia minimo
return Integer.min(altezzaMinimaSinistra, altezzaMinimaDestra) + 1;
}
/*
* TOTALE 2024-01-30
* Realizzare un metodo interno alla classe LinkedBinaryTree che dato l'albero binario
* corrente restituisca il numero di occorrenze di ciascun oggetto contenuto nei nodi dell'albero
*/
public Map<E, Integer> getElementsOccurrency() {
// Creazione Map
Map<E, Integer> mappa = new HashMap<E, Integer>();
if (root == null) return mappa;
getElementsOccurrency(root, mappa);
return mappa;
}
protected void getElementsOccurrency(BinaryNode<E> node, Map<E, Integer> mappa) {
// Se è un nodo nullo
if (node == null) return;
// Si proceda a sx e dx
if (node.getLeft() != null) getElementsOccurrency(node.getLeft(), mappa);
if (node.getRight() != null) getElementsOccurrency(node.getRight(), mappa);
// Si legge l'elemento corrente
E currentData = node.getData();
if (currentData == null) return;
if (mappa.get(currentData) == null) mappa.put(currentData, 1);
else mappa.put(currentData, 1 + mappa.get(currentData));
}
}
@@ -1,6 +1,8 @@
package network.undirected_network;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import network.network.Network;
@@ -43,6 +45,65 @@ public class UndirectedNetwork<Vertex extends Comparable<? super Vertex>> extend
return true;
}
/*
* TOTALE 2024-01-16
* Implementare un metodo interno alla classe UndirectedNetwork
* che aggiunge il numero minimo di archi (con peso 1.0) necessari per rendere il grafo connesso corrente.
*/
/*
* Spiegazione:
* se il grafo ha k componenti connesse,
* servono almeno k - 1 archi per renderlo connesso.
*/
public void connect() {
// Verifica se ci sono elementi nel grafo
if (adjacencyMap.isEmpty()) return;
// Otteniamo il primo vertice per effettuare una BFS
Vertex SS = adjacencyMap.firstKey();
// Segnamo i vertici connessi e quelli non connessi
TreeSet<Vertex> verticiTotali = new TreeSet<Vertex>(adjacencyMap.keySet());
TreeSet<Vertex> verticiConnessi = new TreeSet<Vertex>();
TreeSet<Vertex> verticiSconnessi = new TreeSet<Vertex>();
// Otteniamo i vertici connessi tramite una BFS
Iterator<Vertex> it = breadthFirstIterator(SS);
while (it.hasNext()) verticiConnessi.add(it.next());
// Otteniamo i vertici non connessi
verticiSconnessi.addAll(verticiTotali);
verticiSconnessi.removeAll(verticiConnessi);
// Colleghiamo tutti i vertici non connessi minimamente
while (!verticiSconnessi.isEmpty()) {
Vertex verticeSconnesso = verticiSconnessi.first();
this.addEdge(SS, verticeSconnesso, 1.0);
Iterator<Vertex> itNuoviConnessi = breadthFirstIterator(verticeSconnesso);
while (itNuoviConnessi.hasNext()) verticiConnessi.add(itNuoviConnessi.next());
verticiSconnessi.clear();
verticiSconnessi.addAll(verticiTotali);
verticiSconnessi.removeAll(verticiConnessi);
}
}
/*
* TOTALE 2024-01-30
* Implementare un metodo interno alla classe UndirectedNetwork che restituisce
* il grado del grafo corrente, ovvero il massimo dei gradi dei suoi nodi.
*/
public int graphDegree() {
// Controllo se il grafo è vuoto
if (adjacencyMap.isEmpty()) return 0;
// Conto il grado massimo
Iterator<Vertex> iterator = iterator();
int maxDeg = adjacencyMap.get(iterator.next()).size();
while (iterator.hasNext()) {
int curSize = adjacencyMap.get(iterator.next()).size();
if (curSize > maxDeg) maxDeg = curSize;
}
return maxDeg;
}
}
+57
View File
@@ -0,0 +1,57 @@
package totale.p240116;
public class Docente implements Comparable<Docente>{
// Variables
private String ORCID;
private String nome;
private String cognome;
private int eta;
// Constructor
public Docente(
String ORCID,
String nome,
String cognome,
int eta) {
this.ORCID = ORCID;
this.nome = nome;
this.cognome = cognome;
this.eta = eta;
}
// Getter
public String ORCID() {
return ORCID;
}
public String nome() {
return nome;
}
public String cognome() {
return cognome;
}
public int eta() {
return eta;
}
// Equals
public boolean equals(Object obj) {
if (obj == null) return false;
if (obj == this) return true;
if (!(obj instanceof Docente)) return false;
Docente docente = (Docente) obj;
return this.ORCID.equals(docente.ORCID);
}
// Comparable
@Override
public int compareTo(Docente docente) {
return this.ORCID.compareTo(docente.ORCID);
}
}
+89
View File
@@ -0,0 +1,89 @@
package totale.p240116;
import java.util.Set;
import java.util.TreeSet;
import java.util.Comparator;
import java.util.Iterator;
public class Universita {
Set<Docente> docenti = new TreeSet<Docente>(new Comparator<Docente>() {
@Override
public int compare(Docente d1, Docente d2) {
int cmp;
// By age
cmp = Integer.compare(d1.eta(),d2.eta());
if (cmp != 0) return cmp;
// By surname
cmp = d1.cognome().compareTo(d2.cognome());
if (cmp != 0) return cmp;
// By name
cmp = d1.nome().compareTo(d2.nome());
if (cmp != 0) return cmp;
// Last chance ID
return d1.ORCID().compareTo(d2.ORCID());
}
});
// Method 1
public boolean aggiungi(
String ORCID,
String nome,
String cognome,
int eta) {
// Null check
if (ORCID == null || nome == null || cognome == null) throw new NullPointerException();
// New Object
Docente newDocente = new Docente(ORCID, nome, cognome, eta);
// ID check
for (Docente d : docenti) {
if (d.ORCID().equals(ORCID)) return false;
}
// Add to Set
return docenti.add(newDocente);
}
// Method 2
public boolean presente(String ORCID) {
// Null check
if (ORCID == null) throw new NullPointerException();
// ID check
for (Docente d : docenti) {
if (d.ORCID().equals(ORCID)) return true;
}
// Final result
return false;
}
// Method 3
public boolean rimuovi(String ORCID) {
// Null check
if (ORCID == null) throw new NullPointerException();
// ID remove
Iterator<Docente> it = docenti.iterator();
while (it.hasNext()) {
if (it.next().ORCID().equals(ORCID)) {
it.remove();
return true;
}
}
// If not exists
return false;
}
// Method 4
public int etaMinima() {
// Empty check
if (docenti.isEmpty()) return 0;
// First Docente is minimum age
return docenti.iterator().next().eta();
}
// Method 5
public Set<Docente> rispettoORCID() {
Set<Docente> docenteById = new TreeSet<Docente>();
docenteById.addAll(docenti);
return docenteById;
}
}
+58
View File
@@ -0,0 +1,58 @@
package totale.p240130;
import java.util.Objects;
public class Articolo implements Comparable<Articolo>{
// Variabili
private String id;
private String nome;
private Double prezzo;
// Costruttore
public Articolo(
String id,
String nome,
Double prezzo) {
this.id = id;
this.nome = nome;
this.prezzo = prezzo;
}
// Metodi getter
public String id() {
return id;
}
public String nome() {
return nome;
}
public Double prezzo() {
return prezzo;
}
// Equals
@Override
public boolean equals(Object object) {
if (object == null) return false;
if (object == this) return true;
if (!(object instanceof Articolo)) return false;
Articolo articolo = (Articolo) object;
return this.id.equals(articolo.id);
}
// HashCode
@Override
public int hashCode() {
return Objects.hash(id);
}
// CompareTo
@Override
public int compareTo(Articolo art) {
return this.id.compareTo(art.id);
}
}
+87
View File
@@ -0,0 +1,87 @@
package totale.p240130;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.Comparator;
public class Catologo {
private TreeSet<Articolo> catalogo = new TreeSet<Articolo>(new Comparator<Articolo>() {
@Override
public int compare(Articolo articolo1, Articolo articolo2) {
// Prezzo non decrescente
int cmp = Double.compare(articolo1.prezzo(), articolo2.prezzo());
if (cmp != 0) return cmp;
// Nome crescente
cmp = articolo1.nome().compareTo(articolo2.nome());
if (cmp != 0) return cmp;
// Ritorna in base al ID
return articolo1.id().compareTo(articolo2.id());
}
});
// Metodo 1
public boolean add(Articolo art) {
// Null check
if (art == null) throw new NullPointerException();
// Verifica la presenza
for (Articolo a : catalogo) {
if (a.equals(art)) return false;
}
// Aggiunta
return catalogo.add(art);
}
// Metodo 2
public Articolo cercaArticolo(String cod) {
// Null check
if (cod == null) throw new NullPointerException();
// Ricerca
for (Articolo a : catalogo) {
if (a.id().equals(cod)) return a;
}
return null;
}
// Metodo 3
public boolean cancellaArticolo(String cod) {
// Null check
if (cod == null) throw new NullPointerException();
// Iterator
Iterator<Articolo> it = catalogo.iterator();
while (it.hasNext()) {
Articolo cur = it.next();
if (!(cur.id().equals(cod))) continue;
it.remove();
return true;
}
return false;
}
// Metodo 4
public List<Articolo> getArticoliSottoPrezzo(Double prezzo) {
// Null check
if (prezzo == null) throw new NullPointerException();
// Creazione dell'oggetto di tipo lista
List<Articolo> lista = new LinkedList<Articolo>();
// Controllo sulla size
if (catalogo.isEmpty()) return lista;
// Se il catalogo non è empty
for (Articolo art : catalogo) {
Double curPrezzo = art.prezzo();
if (curPrezzo >= prezzo) break;
lista.add(art);
}
return lista;
}
// Metodo 5
public Set<Articolo> ordinaByCodice() {
Set<Articolo> articoliByCodice = new TreeSet<Articolo>();
articoliByCodice.addAll(catalogo);
return articoliByCodice;
}
}