This commit is contained in:
Emanuele Slusarz
2026-05-11 17:35:58 +02:00
parent 29ef5b6655
commit e4d2eb3ab8
1264 changed files with 30997 additions and 136657 deletions
+2 -2
View File
@@ -1,4 +1,4 @@
#Sun May 10 01:31:23 CEST 2026 #Sun May 10 18:29:39 CEST 2026
host=DESKTOP-ALBQHEK host=DESKTOP-ALBQHEK
process-id=13140 process-id=13748
user=eslusarz user=eslusarz
+95
View File
@@ -617,3 +617,98 @@ java.lang.NullPointerException: Cannot invoke "org.eclipse.jdt.core.dom.Compilat
at org.eclipse.jdt.internal.ui.text.java.hover.BestMatchHover.getHoverInfo2(BestMatchHover.java:131) at org.eclipse.jdt.internal.ui.text.java.hover.BestMatchHover.getHoverInfo2(BestMatchHover.java:131)
at org.eclipse.jdt.internal.ui.text.java.hover.JavaEditorTextHoverProxy.getHoverInfo2(JavaEditorTextHoverProxy.java:89) at org.eclipse.jdt.internal.ui.text.java.hover.JavaEditorTextHoverProxy.getHoverInfo2(JavaEditorTextHoverProxy.java:89)
at org.eclipse.jface.text.TextViewerHoverManager$1.run(TextViewerHoverManager.java:155) at org.eclipse.jface.text.TextViewerHoverManager$1.run(TextViewerHoverManager.java:155)
!ENTRY org.eclipse.jdt.ui 4 0 2026-05-10 16:18:29.461
!MESSAGE AbortCompilation computing hover information in Archivio.java at offset 149
!STACK 0
org.eclipse.jdt.internal.compiler.problem.AbortCompilation:
at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:11585)
at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:11975)
at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:11916)
at org.eclipse.jdt.internal.compiler.parser.Parser.dietParse(Parser.java:10308)
at org.eclipse.jdt.core.dom.CompilationUnitResolver.parse(CompilationUnitResolver.java:656)
at org.eclipse.jdt.core.dom.CompilationUnitResolver.toCompilationUnit(CompilationUnitResolver.java:1432)
at org.eclipse.jdt.core.dom.CompilationUnitResolver$ECJCompilationUnitResolver.toCompilationUnit(CompilationUnitResolver.java:109)
at org.eclipse.jdt.core.dom.ASTParser.internalCreateASTCached(ASTParser.java:1412)
at org.eclipse.jdt.core.dom.ASTParser.lambda$1(ASTParser.java:1291)
at org.eclipse.jdt.internal.core.JavaModelManager.cacheZipFiles(JavaModelManager.java:5709)
at org.eclipse.jdt.core.dom.ASTParser.internalCreateAST(ASTParser.java:1291)
at org.eclipse.jdt.core.dom.ASTParser.createAST(ASTParser.java:933)
at org.eclipse.jdt.core.manipulation.internal.javadoc.CoreJavadocContentAccessUtility.createAST(CoreJavadocContentAccessUtility.java:350)
at org.eclipse.jdt.core.manipulation.internal.javadoc.CoreJavadocContentAccessUtility.getJavadocNode(CoreJavadocContentAccessUtility.java:318)
at org.eclipse.jdt.core.manipulation.internal.javadoc.CoreJavadocAccess.javadoc2HTML(CoreJavadocAccess.java:423)
at org.eclipse.jdt.core.manipulation.internal.javadoc.CoreJavadocAccess.getHTMLContentFromSource(CoreJavadocAccess.java:372)
at org.eclipse.jdt.core.manipulation.internal.javadoc.CoreJavadocAccess.getHTMLContent(CoreJavadocAccess.java:229)
at org.eclipse.jdt.internal.ui.text.javadoc.JavadocContentAccess2.getHTMLContent(JavadocContentAccess2.java:49)
at org.eclipse.jdt.internal.ui.text.java.hover.JavadocHover.getHoverInfo(JavadocHover.java:794)
at org.eclipse.jdt.internal.ui.text.java.hover.JavadocHover.internalGetHoverInfo(JavadocHover.java:712)
at org.eclipse.jdt.internal.ui.text.java.hover.JavadocHover.getHoverInfo2(JavadocHover.java:704)
at org.eclipse.jdt.internal.ui.text.java.hover.BestMatchHover.getHoverInfo2(BestMatchHover.java:165)
at org.eclipse.jdt.internal.ui.text.java.hover.BestMatchHover.getHoverInfo2(BestMatchHover.java:131)
at org.eclipse.jdt.internal.ui.text.java.hover.JavaEditorTextHoverProxy.getHoverInfo2(JavaEditorTextHoverProxy.java:89)
at org.eclipse.jface.text.TextViewerHoverManager$1.run(TextViewerHoverManager.java:155)
Suppressed: java.lang.Throwable: Source line 9 :
-----
package parziale.p191108;
import java.util.HashMap;
import java.util.Map;
public class Archivio {
// Variabili di instanza
private Map<|String, Cliente> mappa = new HashMap<String, Cliente>();
-----
at org.eclipse.jdt.internal.ui.util.SelectionUtil.logException(SelectionUtil.java:157)
at org.eclipse.jdt.internal.ui.text.java.hover.BestMatchHover.getHoverInfo2(BestMatchHover.java:178)
... 3 more
!SESSION 2026-05-10 18:29:11.226 -----------------------------------------------
eclipse.buildId=4.39.0.20260305-0817
java.version=21.0.10
java.vendor=Eclipse Adoptium
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_GB
Framework arguments: -product org.eclipse.epp.package.java.product
Command-line arguments: -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.java.product
!ENTRY ch.qos.logback.classic 1 0 2026-05-10 18:29:35.374
!MESSAGE Activated before the state location was initialized. Retry after the state location is initialized.
!ENTRY ch.qos.logback.classic 1 0 2026-05-10 18:29:41.946
!MESSAGE Logback config file: C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.m2e.logback\logback.2.7.101.20251017-1242.xml
!ENTRY org.eclipse.e4.ui.workbench 4 0 2026-05-10 18:29:45.341
!MESSAGE Unable to load class 'org.eclipse.e4.ui.workbench.renderers.swt.cocoa.CocoaUIHandler' from bundle '147'
!STACK 0
java.lang.ClassNotFoundException: org.eclipse.e4.ui.workbench.renderers.swt.cocoa.CocoaUIHandler cannot be found by org.eclipse.e4.ui.workbench.renderers.swt_0.17.0.v20260131-0926
at org.eclipse.osgi.internal.loader.BundleLoader.generateException(BundleLoader.java:570)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass0(BundleLoader.java:564)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:439)
at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:195)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
at org.eclipse.osgi.internal.framework.EquinoxBundle.loadClass(EquinoxBundle.java:663)
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.createFromBundle(ReflectionContributionFactory.java:88)
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.doCreate(ReflectionContributionFactory.java:59)
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.create(ReflectionContributionFactory.java:37)
at org.eclipse.e4.ui.internal.workbench.swt.E4Application.createE4Workbench(E4Application.java:276)
at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:603)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:339)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:583)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:173)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:185)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:219)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:149)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:115)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:467)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:298)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:615)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:563)
at org.eclipse.equinox.launcher.Main.run(Main.java:1415)
!ENTRY org.eclipse.egit.ui 2 0 2026-05-10 18:30:17.943
!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'C:\Users\eslusarz'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
@@ -0,0 +1,150 @@
package list.mylinkedlist;
import java.util.List;
import java.util.NoSuchElementException;
public class MyLinkedList<E> implements List<E> {
/*
* Classe Nodo
*/
private static class Node<E> {
// Dati del nodo
private E data;
// Riferimenti elemento precedente e successivo
private Node<E> prev;
private Node<E> next;
// Costruttore
public Node(E data, Node<E> prev, Node<E> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
// Costruttore
public MyLinkedList() {}
// Metodi
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public E getFirst() {
if (head == null) throw new NullPointerException();
return head.data;
}
public void addFirst(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
head.prev = newNode;
newNode.next = head;
head = newNode;
}
size++;
}
public E removeFirst() {
if (head == null) throw new NoSuchElementException();
E tmp = head.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> firstPlace = head;
Node<E> secondPlace = head.next;
head = secondPlace;
secondPlace.prev = null;
firstPlace.next = null;
}
size--;
return tmp;
}
public E getLast() {
if (tail == null) throw new NullPointerException();
return tail.data;
}
public E removeLast() {
if (tail == null) throw new NoSuchElementException();
E tmp = tail.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> lastNode = tail;
Node<E> penultimateNode = tail.prev;
tail = penultimateNode;
penultimateNode.next = null;
lastNode.prev = null;
}
size--;
return tmp;
}
public void addLast(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
Node<E> oldLastNode = tail;
oldLastNode.next = newNode;
newNode.prev = oldLastNode;
tail = newNode;
}
size++;
}
public void clear() {
head = tail = null;
size = 0;
}
public boolean add(E item) {
if (item == null) throw new NoSuchElementException();
addLast(item);
return true;
}
public void add(int index, E item) {
if (item == null) throw new NullPointerException();
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
// Se l'inserimento è richiesto nella prima posizione
if (index == 0) {
addFirst(item);
return;
}
// Se l'inserimento è richiesto nell'ultima posizione
if (index == size) {
addLast(item);
return;
}
// Se l'inserimento è richiesto nel generico posto i
Node<E> newNode = new Node<E>(item, null, null);
Node<E> prevNode = head;
for (int i = 0; i < size; i++) {
}
}
}
@@ -1,38 +0,0 @@
package jcf_set.exercise;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IteratoreSenzaDuplicati {
public static void main(String[] main) {
new IteratoreSenzaDuplicati().run();
}
public void run() {
List<String> lista = new ArrayList<String>();
lista.add("tree");
lista.add("flower");
lista.add("tree");
lista.add("flower");
lista.add("animal");
lista.add("flower");
lista.add("fruit");
}
// Esercizio 1
/*
* Ottenere una lista con duplicati.
* Ottenere un iteratore privo di duplicati.
*/
private static <T> Iterator<T> getIteratorNoDuplicates(Iterator<T> it) {
HashSet<T>
}
}
@@ -1,305 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder(node.getLeft(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
inorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
inorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE POSTORDER
/*
* Qui si utilizza il metodo postorder.
* Le regole per il postorder sono queste:
* - Visita prima di tutto ciò che si trova a sinistra
* - Poi visita tutto ciò che si trova a destra
* - Infine visita il sestesso
*/
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
postorder(node.getLeft(), temporaryList);
postorder(node.getRight(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
}
public Iterator<E> iteratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
postorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE LEVEL ORDER
/*
* Qui si usa il metodo levelorder.
* Si analizza tutto il livello dell'albero.
* Per chiarire di seguito un esempio:
*
* A
* / \
* B C
* / \ \
* D E F
*
* La lettura avviene quindi in questo modo:
* A - B - C - D - E - F
*
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
*/
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while(!queueOfNodes.isEmpty()) {
current = queueOfNodes.remove();
if (current.getData() != null) temporaryList.add(current.getData());
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
if (current.getRight() != null) queueOfNodes.add(current.getRight());
}
}
public Iterator<E> iteratorLevelOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
levelorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE
public Iterator<E> iterator() {
return iteratorPreOrder();
}
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso stiamo usando un tipo stack.
* Dobbiamo di fatto simulare uno stack se vogliamo
* iterativamente fare questa cosa.
*
* Perchè se prima dobbiamo visitare il nodo centrale,
* poi il nodo a sinistra e poi il nodo a destra inseriamo
* prima quello di destra e poi quello di sinistra?
*
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
* e quindi l'ultimo che entra è il primo che esce.
*
* dal momento che vale questa cosa, se facciamo entrare prima quello
* di destra e poi quello di sinistra, il primo nodo che verrà
* prelevato è ovviamente quello a sinistra.
*/
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
if (current.getData() != null) temporaryList.add(current.getData());
// Come già spiegato pushamo prima il nodo di destra
if (current.getRight() != null) queueOfNodes.push(current.getRight());
// Infine aggiungiamo il nodo di sinistra
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
}
}
public Iterator<E> ititeratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpreorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
/*
* Questo metodo riguarda lo stesso di quello superiore.
* Il metodo inorder richiede che si visiti prima sinistra,
* poi il nodo stesso, infine il nodo di destra.
*
* Possiamo quindi sempre adoperare uno Stack per siumlare la
* ricorsione.
*
* Come detto prima, se il primo nodo ad essere prelevato è
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
* sinistra -> centro -> destra, il primo nodo da inserire è quello di
* destra
*/
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while () {
}
}
}
@@ -1,31 +0,0 @@
package binary_tree;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>();
}
// METODI
@Override
public boolean isEmpty() {
}
}
@@ -1,327 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder(node.getLeft(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
inorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
inorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE POSTORDER
/*
* Qui si utilizza il metodo postorder.
* Le regole per il postorder sono queste:
* - Visita prima di tutto ciò che si trova a sinistra
* - Poi visita tutto ciò che si trova a destra
* - Infine visita il sestesso
*/
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
postorder(node.getLeft(), temporaryList);
postorder(node.getRight(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
}
public Iterator<E> iteratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
postorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE LEVEL ORDER
/*
* Qui si usa il metodo levelorder.
* Si analizza tutto il livello dell'albero.
* Per chiarire di seguito un esempio:
*
* A
* / \
* B C
* / \ \
* D E F
*
* La lettura avviene quindi in questo modo:
* A - B - C - D - E - F
*
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
*/
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while(!queueOfNodes.isEmpty()) {
current = queueOfNodes.remove();
if (current.getData() != null) temporaryList.add(current.getData());
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
if (current.getRight() != null) queueOfNodes.add(current.getRight());
}
}
public Iterator<E> iteratorLevelOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
levelorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE
public Iterator<E> iterator() {
return iteratorPreOrder();
}
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso stiamo usando un tipo stack.
* Dobbiamo di fatto simulare uno stack se vogliamo
* iterativamente fare questa cosa.
*
* Perchè se prima dobbiamo visitare il nodo centrale,
* poi il nodo a sinistra e poi il nodo a destra inseriamo
* prima quello di destra e poi quello di sinistra?
*
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
* e quindi l'ultimo che entra è il primo che esce.
*
* dal momento che vale questa cosa, se facciamo entrare prima quello
* di destra e poi quello di sinistra, il primo nodo che verrà
* prelevato è ovviamente quello a sinistra.
*/
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
if (current.getData() != null) temporaryList.add(current.getData());
// Come già spiegato pushamo prima il nodo di destra
if (current.getRight() != null) queueOfNodes.push(current.getRight());
// Infine aggiungiamo il nodo di sinistra
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
}
}
public Iterator<E> ititeratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpreorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
/*
* Questo metodo riguarda lo stesso di quello superiore.
* Il metodo inorder richiede che si visiti prima sinistra,
* poi il nodo stesso, infine il nodo di destra.
*
* Possiamo quindi sempre adoperare uno Stack per siumlare la
* ricorsione.
*
* Come detto prima, se il primo nodo ad essere prelevato è
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
* sinistra -> centro -> destra, il primo nodo da inserire è quello di
* destra
*/
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.add(node);
flags.push(false);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
if (current.getData() != null) temporaryList.add(current.getData());
} else {
// il nodo non è da visitare
// prima si aggiunge il nodo di destra
if (current.getRight() != null) {
queueOfNodes.add(current.getRight());
flags.add(false);
}
}
}
}
}
@@ -1,317 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder(node.getLeft(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
inorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
inorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE POSTORDER
/*
* Qui si utilizza il metodo postorder.
* Le regole per il postorder sono queste:
* - Visita prima di tutto ciò che si trova a sinistra
* - Poi visita tutto ciò che si trova a destra
* - Infine visita il sestesso
*/
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
postorder(node.getLeft(), temporaryList);
postorder(node.getRight(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
}
public Iterator<E> iteratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
postorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE LEVEL ORDER
/*
* Qui si usa il metodo levelorder.
* Si analizza tutto il livello dell'albero.
* Per chiarire di seguito un esempio:
*
* A
* / \
* B C
* / \ \
* D E F
*
* La lettura avviene quindi in questo modo:
* A - B - C - D - E - F
*
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
*/
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while(!queueOfNodes.isEmpty()) {
current = queueOfNodes.remove();
if (current.getData() != null) temporaryList.add(current.getData());
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
if (current.getRight() != null) queueOfNodes.add(current.getRight());
}
}
public Iterator<E> iteratorLevelOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
levelorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE
public Iterator<E> iterator() {
return iteratorPreOrder();
}
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso stiamo usando un tipo stack.
* Dobbiamo di fatto simulare uno stack se vogliamo
* iterativamente fare questa cosa.
*
* Perchè se prima dobbiamo visitare il nodo centrale,
* poi il nodo a sinistra e poi il nodo a destra inseriamo
* prima quello di destra e poi quello di sinistra?
*
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
* e quindi l'ultimo che entra è il primo che esce.
*
* dal momento che vale questa cosa, se facciamo entrare prima quello
* di destra e poi quello di sinistra, il primo nodo che verrà
* prelevato è ovviamente quello a sinistra.
*/
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
if (current.getData() != null) temporaryList.add(current.getData());
// Come già spiegato pushamo prima il nodo di destra
if (current.getRight() != null) queueOfNodes.push(current.getRight());
// Infine aggiungiamo il nodo di sinistra
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
}
}
public Iterator<E> ititeratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpreorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
/*
* Questo metodo riguarda lo stesso di quello superiore.
* Il metodo inorder richiede che si visiti prima sinistra,
* poi il nodo stesso, infine il nodo di destra.
*
* Possiamo quindi sempre adoperare uno Stack per siumlare la
* ricorsione.
*
* Come detto prima, se il primo nodo ad essere prelevato è
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
* sinistra -> centro -> destra, il primo nodo da inserire è quello di
* destra
*/
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.add(node);
flags.push(false);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
} else {
// il nodo non è da visitare
}
}
}
}
@@ -1,92 +0,0 @@
package binary_tree;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
}
}
@@ -1,40 +0,0 @@
package binary_tree;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
}
}
@@ -0,0 +1,98 @@
package list.mylinkedlist;
import java.util.List;
import java.util.NoSuchElementException;
public class MyLinkedList<E> implements List<E> {
/*
* Classe Nodo
*/
private static class Node<E> {
// Dati del nodo
private E data;
// Riferimenti elemento precedente e successivo
private Node<E> prev;
private Node<E> next;
// Costruttore
public Node(E data, Node<E> prev, Node<E> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
// Costruttore
public MyLinkedList() {}
// Metodi
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public E getFirst() {
if (head == null) throw new NullPointerException();
return head.data;
}
public void addFirst(E item) {
if (item == null) throw new NullPointerException();
if (size == 0) {
head = tail = new Node<E>(item, null, null);
} else {
Node<E> newNode = new Node<E>(item, null, null);
head.prev = newNode;
newNode.next = head;
head = newNode;
}
size++;
}
public E removeFirst() {
if (head == null) throw new NoSuchElementException();
E tmp = head.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> firstPlace = head;
Node<E> secondPlace = head.next;
head = secondPlace;
secondPlace.prev = null;
firstPlace.next = null;
}
size--;
return tmp;
}
public E getLast() {
if (tail == null) throw new NullPointerException();
return tail.data;
}
public E removeLast() {
if (tail == null) throw new NoSuchElementException();
E tmp = tail.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> lastNode = tail;
}
size--;
return tmp;
}
}
@@ -1,16 +0,0 @@
package binary_tree;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
// METODI
@Override
public boolean isEmpty() {
}
}
@@ -1,468 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder(node.getLeft(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
inorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
inorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE POSTORDER
/*
* Qui si utilizza il metodo postorder.
* Le regole per il postorder sono queste:
* - Visita prima di tutto ciò che si trova a sinistra
* - Poi visita tutto ciò che si trova a destra
* - Infine visita il sestesso
*/
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
postorder(node.getLeft(), temporaryList);
postorder(node.getRight(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
}
public Iterator<E> iteratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
postorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE LEVEL ORDER
/*
* Qui si usa il metodo levelorder.
* Si analizza tutto il livello dell'albero.
* Per chiarire di seguito un esempio:
*
* A
* / \
* B C
* / \ \
* D E F
*
* La lettura avviene quindi in questo modo:
* A - B - C - D - E - F
*
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
*/
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while(!queueOfNodes.isEmpty()) {
current = queueOfNodes.remove();
if (current.getData() != null) temporaryList.add(current.getData());
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
if (current.getRight() != null) queueOfNodes.add(current.getRight());
}
}
public Iterator<E> iteratorLevelOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
levelorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE
public Iterator<E> iterator() {
return iteratorPreOrder();
}
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso stiamo usando un tipo stack.
* Dobbiamo di fatto simulare uno stack se vogliamo
* iterativamente fare questa cosa.
*
* Perchè se prima dobbiamo visitare il nodo centrale,
* poi il nodo a sinistra e poi il nodo a destra inseriamo
* prima quello di destra e poi quello di sinistra?
*
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
* e quindi l'ultimo che entra è il primo che esce.
*
* dal momento che vale questa cosa, se facciamo entrare prima quello
* di destra e poi quello di sinistra, il primo nodo che verrà
* prelevato è ovviamente quello a sinistra.
*/
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
if (current.getData() != null) temporaryList.add(current.getData());
// Come già spiegato pushamo prima il nodo di destra
if (current.getRight() != null) queueOfNodes.push(current.getRight());
// Infine aggiungiamo il nodo di sinistra
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
}
}
public Iterator<E> ititeratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpreorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
/*
* Questo metodo riguarda lo stesso di quello superiore.
* Il metodo inorder richiede che si visiti prima sinistra,
* poi il nodo stesso, infine il nodo di destra.
*
* Possiamo quindi sempre adoperare uno Stack per siumlare la
* ricorsione.
*
* Come detto prima, se il primo nodo ad essere prelevato è
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
* sinistra -> centro -> destra.
*
* Nel metodo sono state inserite delle check con uno stack di
* flag che permette di capire quando il nodo centrale è visitare
* oppure no.
*/
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.add(node);
flags.push(false);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
if (current.getData() != null) temporaryList.add(current.getData());
} else {
// il nodo non è da visitare
// prima si aggiunge il nodo di destra
if (current.getRight() != null) {
queueOfNodes.add(current.getRight());
flags.add(false);
}
queueOfNodes.push(current);
flags.push(true);
if (current.getLeft() != null) {
queueOfNodes.add(current.getLeft());
flags.push(false);
}
}
}
}
public Iterator<E> ititeratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itinorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR POST ORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso la logica è la seguente. si ha la
* necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE
*
* Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario,
* dunque NODO CORRENTE -> DESTRA -> SINISTRA
*
* Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra.
*/
protected void itpostorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.push(node);
flags.push(false);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
if (current.getData() != null) temporaryList.add(current.getData());
} else {
// il nodo non è ancora da visitare
// Visitiamo il nodo corrente
queueOfNodes.push(current);
flags.push(true);
// visitiamo il nodo di destra
if (current.getRight() != null) {
queueOfNodes.push(current.getRight());
flags.push(false);
}
// visitiamo il nodo di sinistra infine
if (current.getLeft() != null) {
queueOfNodes.push(current.getLeft());
flags.push(false);
}
}
}
}
public Iterator<E> ititeratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpostorder(root, temporaryList);
return temporaryList.iterator();
}
/*
* II parziale 2022/2023
* Realizzare un metodo costruttore della classe LinkedBinaryTree<E> che
* prende in input una lista di oggetti di tipo E e costruisce una catena
* casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo
* della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro.
*/
/*
* Spiegazione intuitiva (visto che la melideo intende complicare la consegna):
* Data ad esempio una lista del genere:
* [A, B, C, D]
*
* Si ottiene quindi una catena del genere:
* A
* \
* B
* /
* C
* \
* D
*/
public LinkedBinaryTree(List<E> objectList) {
if (objectList == null) throw new NullPointerException();
Iterator<E> iterator = objectList.iterator();
if (!iterator.hasNext()) return;
E currentObject = iterator.next();
root = new BinaryNode<E>(currentObject);
BinaryNode<E> currentNode = root;
currentNode.setData(currentObject);
size = 1;
while (iterator.hasNext()) {
currentObject = iterator.next();
int wing = (int) (Math.random() * 2);
BinaryNode<E> newNode = new BinaryNode<E>(currentObject);
if (wing == 0) {
// Left
newNode.setParentAsLeftChild(currentNode);
} else {
// Right
newNode.setParentAsRightChild(currentNode);
}
currentNode = newNode;
size++;
}
}
/*
* II parziale 2022
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo
* che stampa le foglie dall'albero corrente (da sinistra verso destra)
*/
public void printLeaf() {
}
public void printLeaf(BinaryNode<E> node) {
if (node == null) return;
if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString());
else {
printLeaf(node.getLeft());
}
}
}
@@ -1,51 +0,0 @@
package binary_tree;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
}
@@ -0,0 +1,150 @@
package list.mylinkedlist;
import java.util.List;
import java.util.NoSuchElementException;
public class MyLinkedList<E> implements List<E> {
/*
* Classe Nodo
*/
private static class Node<E> {
// Dati del nodo
private E data;
// Riferimenti elemento precedente e successivo
private Node<E> prev;
private Node<E> next;
// Costruttore
public Node(E data, Node<E> prev, Node<E> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
// Costruttore
public MyLinkedList() {}
// Metodi
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public E getFirst() {
if (head == null) throw new NullPointerException();
return head.data;
}
public void addFirst(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
head.prev = newNode;
newNode.next = head;
head = newNode;
}
size++;
}
public E removeFirst() {
if (head == null) throw new NoSuchElementException();
E tmp = head.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> firstPlace = head;
Node<E> secondPlace = head.next;
head = secondPlace;
secondPlace.prev = null;
firstPlace.next = null;
}
size--;
return tmp;
}
public E getLast() {
if (tail == null) throw new NullPointerException();
return tail.data;
}
public E removeLast() {
if (tail == null) throw new NoSuchElementException();
E tmp = tail.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> lastNode = tail;
Node<E> penultimateNode = tail.prev;
tail = penultimateNode;
penultimateNode.next = null;
lastNode.prev = null;
}
size--;
return tmp;
}
public void addLast(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
Node<E> oldLastNode = tail;
oldLastNode.next = newNode;
newNode.prev = oldLastNode;
tail = newNode;
}
size++;
}
public void clear() {
head = tail = null;
size = 0;
}
public boolean add(E item) {
if (item == null) throw new NoSuchElementException();
addLast(item);
return true;
}
public void add(int index, E item) {
if (item == null) throw new NullPointerException();
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
// Se l'inserimento è richiesto nella prima posizione
if (index == 0) {
addFirst(item);
return;
}
// Se l'inserimento è richiesto nell'ultima posizione
if (index == size) {
addLast(item);
return;
}
// Se l'inserimento è richiesto nel generico posto i
Node<E> newNode = new Node<E>(item, null, null);
Node<E> prevNode = head;
for (int i = 0; i < index; i++) {
prevNode = prevNode.next;
}
}
}
@@ -1,129 +0,0 @@
package binary_tree;
import java.util.List;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData != null)
}
}
@@ -1,78 +0,0 @@
package binary_tree;
public class BinaryNode<E> {
// VARIABILI
private BinaryNode<E> left, right, parent;
private E data;
// COSTRUTTORI
public BinaryNode(E data) {
this.data = data;
left = right = parent = null;
}
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
this.data = data;
this.left = left;
this.right = right;
this.parent = null;
if (this.left != null) this.left.parent = this;
if (this.right != null) this.right.parent = this;
}
// METODI
public E getData() {
return data;
}
public BinaryNode<E> getLeft() {
return left;
}
public BinaryNode<E> getRight() {
return right;
}
public BinaryNode<E> getParent() {
return parent;
}
public boolean isRoot() {
return parent == null;
}
public boolean isLeftChild() {
return parent != null && parent.getLeft() == this;
}
public boolean isRightChild() {
return parent != null && parent.getRight() == this;
}
public void setData(E data) {
this.data = data;
}
public void setLeft(BinaryNode<E> left) {
this.left = left;
if (this.left != null) this.left.parent = this;
}
public void setRight(BinaryNode<E> right) {
this.right = right;
if (this.right != null) this.right.parent = this;
}
public BinaryNode<E> setParentAsLeftChild(BinaryNode<E> parent) {
}
}
@@ -1,5 +0,0 @@
package vettore_ordinabile;
public class VettoriIntero {
}
@@ -1,38 +0,0 @@
package jcf_set.exercise;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IteratoreSenzaDuplicati {
public static void main(String[] main) {
new IteratoreSenzaDuplicati().run();
}
public void run() {
List<String> lista = new ArrayList<String>();
lista.add("tree");
lista.add("flower");
lista.add("tree");
lista.add("flower");
lista.add("animal");
lista.add("flower");
lista.add("fruit");
}
// Esercizio 1
/*
* Ottenere una lista con duplicati.
* Ottenere un iteratore privo di duplicati.
*/
private static <T> Iterator<T> getIteratorNoDuplicates(Iterator<T> it) {
HashSet<T
}
}
@@ -0,0 +1,154 @@
package list.mylinkedlist;
import java.util.List;
import java.util.NoSuchElementException;
public class MyLinkedList<E> implements List<E> {
/*
* Classe Nodo
*/
private static class Node<E> {
// Dati del nodo
private E data;
// Riferimenti elemento precedente e successivo
private Node<E> prev;
private Node<E> next;
// Costruttore
public Node(E data, Node<E> prev, Node<E> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
// Costruttore
public MyLinkedList() {}
// Metodi
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public E getFirst() {
if (head == null) throw new NullPointerException();
return head.data;
}
public void addFirst(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
head.prev = newNode;
newNode.next = head;
head = newNode;
}
size++;
}
public E removeFirst() {
if (head == null) throw new NoSuchElementException();
E tmp = head.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> firstPlace = head;
Node<E> secondPlace = head.next;
head = secondPlace;
secondPlace.prev = null;
firstPlace.next = null;
}
size--;
return tmp;
}
public E getLast() {
if (tail == null) throw new NullPointerException();
return tail.data;
}
public E removeLast() {
if (tail == null) throw new NoSuchElementException();
E tmp = tail.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> lastNode = tail;
Node<E> penultimateNode = tail.prev;
tail = penultimateNode;
penultimateNode.next = null;
lastNode.prev = null;
}
size--;
return tmp;
}
public void addLast(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
Node<E> oldLastNode = tail;
oldLastNode.next = newNode;
newNode.prev = oldLastNode;
tail = newNode;
}
size++;
}
public void clear() {
head = tail = null;
size = 0;
}
public boolean add(E item) {
if (item == null) throw new NoSuchElementException();
addLast(item);
return true;
}
public void add(int index, E item) {
if (item == null) throw new NullPointerException();
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
// Se l'inserimento è richiesto nella prima posizione
if (index == 0) {
addFirst(item);
return;
}
// Se l'inserimento è richiesto nell'ultima posizione
if (index == size) {
addLast(item);
return;
}
// Se l'inserimento è richiesto nel generico posto i
Node<E> newNode = new Node<E>(item, null, null);
Node<E> prevNode = head;
for (int i = 0; i < index - 1; i++) {
prevNode = prevNode.next;
}
Node<E> nextNode = prevNode.next;
prevNode.next = newNode;
}
}
@@ -1,307 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder(node.getLeft(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
inorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
inorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE POSTORDER
/*
* Qui si utilizza il metodo postorder.
* Le regole per il postorder sono queste:
* - Visita prima di tutto ciò che si trova a sinistra
* - Poi visita tutto ciò che si trova a destra
* - Infine visita il sestesso
*/
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
postorder(node.getLeft(), temporaryList);
postorder(node.getRight(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
}
public Iterator<E> iteratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
postorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE LEVEL ORDER
/*
* Qui si usa il metodo levelorder.
* Si analizza tutto il livello dell'albero.
* Per chiarire di seguito un esempio:
*
* A
* / \
* B C
* / \ \
* D E F
*
* La lettura avviene quindi in questo modo:
* A - B - C - D - E - F
*
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
*/
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while(!queueOfNodes.isEmpty()) {
current = queueOfNodes.remove();
if (current.getData() != null) temporaryList.add(current.getData());
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
if (current.getRight() != null) queueOfNodes.add(current.getRight());
}
}
public Iterator<E> iteratorLevelOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
levelorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE
public Iterator<E> iterator() {
return iteratorPreOrder();
}
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso stiamo usando un tipo stack.
* Dobbiamo di fatto simulare uno stack se vogliamo
* iterativamente fare questa cosa.
*
* Perchè se prima dobbiamo visitare il nodo centrale,
* poi il nodo a sinistra e poi il nodo a destra inseriamo
* prima quello di destra e poi quello di sinistra?
*
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
* e quindi l'ultimo che entra è il primo che esce.
*
* dal momento che vale questa cosa, se facciamo entrare prima quello
* di destra e poi quello di sinistra, il primo nodo che verrà
* prelevato è ovviamente quello a sinistra.
*/
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
if (current.getData() != null) temporaryList.add(current.getData());
// Come già spiegato pushamo prima il nodo di destra
if (current.getRight() != null) queueOfNodes.push(current.getRight());
// Infine aggiungiamo il nodo di sinistra
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
}
}
public Iterator<E> ititeratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpreorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
/*
* Questo metodo riguarda lo stesso di quello superiore.
* Il metodo inorder richiede che si visiti prima sinistra,
* poi il nodo stesso, infine il nodo di destra.
*
* Possiamo quindi sempre adoperare uno Stack per siumlare la
* ricorsione.
*
* Come detto prima, se il primo nodo ad essere prelevato è
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
* sinistra -> centro -> destra, il primo nodo da inserire è quello di
* destra
*/
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
if ()
}
}
}
@@ -1,439 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder(node.getLeft(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
inorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
inorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE POSTORDER
/*
* Qui si utilizza il metodo postorder.
* Le regole per il postorder sono queste:
* - Visita prima di tutto ciò che si trova a sinistra
* - Poi visita tutto ciò che si trova a destra
* - Infine visita il sestesso
*/
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
postorder(node.getLeft(), temporaryList);
postorder(node.getRight(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
}
public Iterator<E> iteratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
postorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE LEVEL ORDER
/*
* Qui si usa il metodo levelorder.
* Si analizza tutto il livello dell'albero.
* Per chiarire di seguito un esempio:
*
* A
* / \
* B C
* / \ \
* D E F
*
* La lettura avviene quindi in questo modo:
* A - B - C - D - E - F
*
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
*/
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while(!queueOfNodes.isEmpty()) {
current = queueOfNodes.remove();
if (current.getData() != null) temporaryList.add(current.getData());
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
if (current.getRight() != null) queueOfNodes.add(current.getRight());
}
}
public Iterator<E> iteratorLevelOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
levelorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE
public Iterator<E> iterator() {
return iteratorPreOrder();
}
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso stiamo usando un tipo stack.
* Dobbiamo di fatto simulare uno stack se vogliamo
* iterativamente fare questa cosa.
*
* Perchè se prima dobbiamo visitare il nodo centrale,
* poi il nodo a sinistra e poi il nodo a destra inseriamo
* prima quello di destra e poi quello di sinistra?
*
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
* e quindi l'ultimo che entra è il primo che esce.
*
* dal momento che vale questa cosa, se facciamo entrare prima quello
* di destra e poi quello di sinistra, il primo nodo che verrà
* prelevato è ovviamente quello a sinistra.
*/
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
if (current.getData() != null) temporaryList.add(current.getData());
// Come già spiegato pushamo prima il nodo di destra
if (current.getRight() != null) queueOfNodes.push(current.getRight());
// Infine aggiungiamo il nodo di sinistra
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
}
}
public Iterator<E> ititeratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpreorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
/*
* Questo metodo riguarda lo stesso di quello superiore.
* Il metodo inorder richiede che si visiti prima sinistra,
* poi il nodo stesso, infine il nodo di destra.
*
* Possiamo quindi sempre adoperare uno Stack per siumlare la
* ricorsione.
*
* Come detto prima, se il primo nodo ad essere prelevato è
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
* sinistra -> centro -> destra.
*
* Nel metodo sono state inserite delle check con uno stack di
* flag che permette di capire quando il nodo centrale è visitare
* oppure no.
*/
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.add(node);
flags.push(false);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
if (current.getData() != null) temporaryList.add(current.getData());
} else {
// il nodo non è da visitare
// prima si aggiunge il nodo di destra
if (current.getRight() != null) {
queueOfNodes.add(current.getRight());
flags.add(false);
}
queueOfNodes.push(current);
flags.push(true);
if (current.getLeft() != null) {
queueOfNodes.add(current.getLeft());
flags.push(false);
}
}
}
}
public Iterator<E> ititeratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itinorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR POST ORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso la logica è la seguente. si ha la
* necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE
*
* Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario,
* dunque NODO CORRENTE -> DESTRA -> SINISTRA
*
* Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra.
*/
protected void itpostorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.push(node);
flags.push(false);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
if (current.getData() != null) temporaryList.add(current.getData());
} else {
// il nodo non è ancora da visitare
// Visitiamo il nodo corrente
queueOfNodes.push(current);
flags.push(true);
// visitiamo il nodo di destra
if (current.getRight() != null) {
queueOfNodes.push(current.getRight());
flags.push(false);
}
// visitiamo il nodo di sinistra infine
if (current.getLeft() != null) {
queueOfNodes.push(current.getLeft());
flags.push(false);
}
}
}
}
public Iterator<E> ititeratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpostorder(root, temporaryList);
return temporaryList.iterator();
}
/*
* II parziale 2022/2023
* Realizzare un metodo costruttore della classe LinkedBinaryTree<E> che
* prende in input una lista di oggetti di tipo E e costruisce una catena
* casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo
* della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro.
*/
/*
* Spiegazione intuitiva (visto che la melideo intende complicare la consegna):
* Data ad esempio una lista del genere:
* [A, B, C, D]
*
* Si ottiene quindi una catena del genere:
* A
* \
* B
* /
* C
* \
* D
*/
public LinkedBinaryTree(List<E> objectList) {
if (objectList == null) throw new NullPointerException();
Iterator<E> iterator = objectList.iterator();
BinaryNode<E> currentNode = root;
while (iterator.hasNext()) {
E currentObject = iterator.next();
int wing = (int) (Math.random() * 2);
currentNode.setData(currentObject);
if (wing == 0) {
} else {
}
}
}
}
@@ -0,0 +1,60 @@
package parziale.p191108;
import java.util.HashMap;
import java.util.Map;
public class Archivio {
// Variabili di instanza
private Map<String, Cliente> archivio = new HashMap<String, Cliente>();
// Metodi
// 2. Inserimento nell'archivio
/*
* Inserimento nell'archio attuale di una nuova associazione,
* dati la targa dell'automobile(String) e un riferimento al proprietario
* della classe Cliente: se l'automobile è già presente, si aggiorni l'informazione
* relativa al proprietario
*/
public void aggiungi(String targa, Cliente cliente) {
if (targa == null || cliente == null) throw new NullPointerException();
targa = targa.trim();
if (targa.isEmpty()) throw new IllegalArgumentException();
archivio.put(targa, cliente);
}
// 3. Cancellazione dall'archivio attuale di un'automobile, data la sua targa
public void remove(String targa) {
if (targa == null) throw new NullPointerException();
targa.trim();
if (targa.isEmpty()) throw new IllegalArgumentException();
archivio.remove(targa);
}
// 4. Creazione di un nuovo archivio contenete le automobili selezionate dall'archivio principale aventi il proprietario residente in una data città.
public Map<String, Cliente> creaArchivoPerCitta(String citta) {
if (citta == null) throw new NullPointerException();
HashMap<String, Cliente> autoSelezionate = new HashMap<String, Cliente>();
for (String s : archivio.keySet()) {
Cliente currentCliente = archivio.get(s);
String currentCitta = currentCliente.cittaResidenza();
if (currentCitta.equals(citta)) autoSelezionate.put(s, currentCliente);
}
return autoSelezionate;
}
// 5. Ordinamento dellarchivio in modo lessicografico crescente rispetto alla targa delle automobili
public Map<String, Cliente> archivioOrdinatoRispettoAllaTarga() {
TreeSet<String, Cliente> archivioOrdinato = new TreeSet<String, Cliente>();
}
}
@@ -0,0 +1,42 @@
package parziale.p191108;
import java.util.HashMap;
import java.util.Map;
public class Archivio {
// Variabili di instanza
private Map<String, Cliente> archivio = new HashMap<String, Cliente>();
// Metodi
// 2. Inserimento nell'archivio
/*
* Inserimento nell'archio attuale di una nuova associazione,
* dati la targa dell'automobile(String) e un riferimento al proprietario
* della classe Cliente: se l'automobile è già presente, si aggiorni l'informazione
* relativa al proprietario
*/
public void aggiungi(String targa, Cliente cliente) {
if (targa == null || cliente == null) throw new NullPointerException();
targa = targa.trim();
if (targa.isEmpty()) throw new IllegalArgumentException();
archivio.put(targa, cliente);
}
// 3. Cancellazione dall'archivio attuale di un'automobile, data la sua targa
public void remove(String targa) {
if (targa == null) throw new NullPointerException();
targa.trim();
if (targa.isEmpty()) throw new IllegalArgumentException();
archivio.remove(targa);
}
// 4. Creazione di un nuovo archivio contenete le automobili selezionate dall'archivio principale aventi il proprietario residente in un'altra città.
}
@@ -0,0 +1,9 @@
package jcf_map.exercise;
public class BinaryTreeMap {
/*
* Esercizio 1 - Esame del secondo parziale 8/1/2019
* Scrivere un metodo generico statico ContaDuplicati che conta il numero
* di oggetti duplicati (non univoci) contenuti in un albero binario.
*/
}
@@ -1,91 +0,0 @@
package binary_tree;
public class BinaryNode<E> {
// VARIABILI
private BinaryNode<E> left, right, parent;
private E data;
// COSTRUTTORI
public BinaryNode(E data) {
this.data = data;
left = right = parent = null;
}
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
this.data = data;
this.left = left;
this.right = right;
this.parent = null;
if (this.left != null) this.left.parent = this;
if (this.right != null) this.right.parent = this;
}
// METODI
public E getData() {
return data;
}
public BinaryNode<E> getLeft() {
return left;
}
public BinaryNode<E> getRight() {
return right;
}
public BinaryNode<E> getParent() {
return parent;
}
public boolean isRoot() {
return parent == null;
}
public boolean isLeftChild() {
return parent != null && parent.getLeft() == this;
}
public boolean isRightChild() {
return parent != null && parent.getRight() == this;
}
public void setData(E data) {
this.data = data;
}
public void setLeft(BinaryNode<E> left) {
this.left = left;
if (this.left != null) this.left.parent = this;
}
public void setRight(BinaryNode<E> right) {
this.right = right;
if (this.right != null) this.right.parent = this;
}
public BinaryNode<E> setParentAsLeftChild(BinaryNode<E> parent) {
if (parent == null) return null;
this.parent = parent;
BinaryNode<E> oldLeft = this.parent.left;
this.parent.left = this;
return oldLeft;
}
public BinaryNode<E> setParentAsRightChild(BinaryNode<E> parent) {
if (parent == null) return null;
this.parent = parent;
BinaryNode<E> oldRight = this.parent.right;
}
}
@@ -1,68 +0,0 @@
package binary_tree;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree.root = root.getLeft();
return leftTree;
}
}
@@ -1,26 +0,0 @@
package jcf_set.exercise;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Insiemistica {
public static void main(String[] main) {
// Insiemi di test
Integer[] aArray = {1, -2, 3, -4, 3};
Integer[] bArray = {1, 5, -6, -4, 5};
// Input come HashSet (Ordine non garantito)
Set<Integer> aHashSet = new HashSet<Integer>(Arrays.asList(aArray));
Set<Integer> bHashSet = new HashSet<Integer>(Arrays.asList(bArray));
}
// Operazioni di insiemistica
}
@@ -1,12 +0,0 @@
package jcf_set.exercise;
public class Insiemistica {
public static void main(String[] main) {
// Insiemi di test
Integer[] aArray = {1, -2, 3, -4, 3};
}
}
@@ -1,74 +0,0 @@
package binary_tree;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
}
@@ -1,488 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder(node.getLeft(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
inorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
inorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE POSTORDER
/*
* Qui si utilizza il metodo postorder.
* Le regole per il postorder sono queste:
* - Visita prima di tutto ciò che si trova a sinistra
* - Poi visita tutto ciò che si trova a destra
* - Infine visita il sestesso
*/
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
postorder(node.getLeft(), temporaryList);
postorder(node.getRight(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
}
public Iterator<E> iteratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
postorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE LEVEL ORDER
/*
* Qui si usa il metodo levelorder.
* Si analizza tutto il livello dell'albero.
* Per chiarire di seguito un esempio:
*
* A
* / \
* B C
* / \ \
* D E F
*
* La lettura avviene quindi in questo modo:
* A - B - C - D - E - F
*
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
*/
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while(!queueOfNodes.isEmpty()) {
current = queueOfNodes.remove();
if (current.getData() != null) temporaryList.add(current.getData());
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
if (current.getRight() != null) queueOfNodes.add(current.getRight());
}
}
public Iterator<E> iteratorLevelOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
levelorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE
public Iterator<E> iterator() {
return iteratorPreOrder();
}
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso stiamo usando un tipo stack.
* Dobbiamo di fatto simulare uno stack se vogliamo
* iterativamente fare questa cosa.
*
* Perchè se prima dobbiamo visitare il nodo centrale,
* poi il nodo a sinistra e poi il nodo a destra inseriamo
* prima quello di destra e poi quello di sinistra?
*
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
* e quindi l'ultimo che entra è il primo che esce.
*
* dal momento che vale questa cosa, se facciamo entrare prima quello
* di destra e poi quello di sinistra, il primo nodo che verrà
* prelevato è ovviamente quello a sinistra.
*/
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
if (current.getData() != null) temporaryList.add(current.getData());
// Come già spiegato pushamo prima il nodo di destra
if (current.getRight() != null) queueOfNodes.push(current.getRight());
// Infine aggiungiamo il nodo di sinistra
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
}
}
public Iterator<E> ititeratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpreorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
/*
* Questo metodo riguarda lo stesso di quello superiore.
* Il metodo inorder richiede che si visiti prima sinistra,
* poi il nodo stesso, infine il nodo di destra.
*
* Possiamo quindi sempre adoperare uno Stack per siumlare la
* ricorsione.
*
* Come detto prima, se il primo nodo ad essere prelevato è
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
* sinistra -> centro -> destra.
*
* Nel metodo sono state inserite delle check con uno stack di
* flag che permette di capire quando il nodo centrale è visitare
* oppure no.
*/
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.add(node);
flags.push(false);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
if (current.getData() != null) temporaryList.add(current.getData());
} else {
// il nodo non è da visitare
// prima si aggiunge il nodo di destra
if (current.getRight() != null) {
queueOfNodes.add(current.getRight());
flags.add(false);
}
queueOfNodes.push(current);
flags.push(true);
if (current.getLeft() != null) {
queueOfNodes.add(current.getLeft());
flags.push(false);
}
}
}
}
public Iterator<E> ititeratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itinorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR POST ORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso la logica è la seguente. si ha la
* necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE
*
* Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario,
* dunque NODO CORRENTE -> DESTRA -> SINISTRA
*
* Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra.
*/
protected void itpostorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.push(node);
flags.push(false);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
if (current.getData() != null) temporaryList.add(current.getData());
} else {
// il nodo non è ancora da visitare
// Visitiamo il nodo corrente
queueOfNodes.push(current);
flags.push(true);
// visitiamo il nodo di destra
if (current.getRight() != null) {
queueOfNodes.push(current.getRight());
flags.push(false);
}
// visitiamo il nodo di sinistra infine
if (current.getLeft() != null) {
queueOfNodes.push(current.getLeft());
flags.push(false);
}
}
}
}
public Iterator<E> ititeratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpostorder(root, temporaryList);
return temporaryList.iterator();
}
/*
* II parziale 2022/2023
* Realizzare un metodo costruttore della classe LinkedBinaryTree<E> che
* prende in input una lista di oggetti di tipo E e costruisce una catena
* casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo
* della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro.
*/
/*
* Spiegazione intuitiva (visto che la melideo intende complicare la consegna):
* Data ad esempio una lista del genere:
* [A, B, C, D]
*
* Si ottiene quindi una catena del genere:
* A
* \
* B
* /
* C
* \
* D
*/
public LinkedBinaryTree(List<E> objectList) {
if (objectList == null) throw new NullPointerException();
Iterator<E> iterator = objectList.iterator();
if (!iterator.hasNext()) return;
E currentObject = iterator.next();
root = new BinaryNode<E>(currentObject);
BinaryNode<E> currentNode = root;
currentNode.setData(currentObject);
size = 1;
while (iterator.hasNext()) {
currentObject = iterator.next();
int wing = (int) (Math.random() * 2);
BinaryNode<E> newNode = new BinaryNode<E>(currentObject);
if (wing == 0) {
// Left
newNode.setParentAsLeftChild(currentNode);
} else {
// Right
newNode.setParentAsRightChild(currentNode);
}
currentNode = newNode;
size++;
}
}
/*
* II parziale 2022
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo
* che stampa le foglie dall'albero corrente (da sinistra verso destra)
*/
public void printLeaf() {
printLeaf(root);
}
public void printLeaf(BinaryNode<E> node) {
if (node == null) return;
if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString());
else {
printLeaf(node.getLeft());
printLeaf(node.getRight());
}
}
/*
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo che conta il numero
* di foglie dell'albero corrente
*/
public void numberLeaf() {
Integer number = 0;
numberLeaf(root, number);
}
public void numberLeaf(BinaryNode<E> node, Integer number) {
if (node == null) return;
if (node.getLeft() == null && node.getRight() == null) number++;
else {
numberLeaf(node.getLeft(), number);
numberLeaf(node.getRight(), number);
}
}
}
@@ -0,0 +1,27 @@
package jcf_map.exercise;
import java.util.HashMap;
import java.util.Map;
import binary_tree.BinaryNode;
public class BinaryTreeMap {
/*
* Esercizio 1 - Esame del secondo parziale 8/1/2019
* Scrivere un metodo generico statico ContaDuplicati che conta il numero
* di oggetti duplicati (non univoci) contenuti in un albero binario.
* Il risultato è il conteggio totale degli elementi che risultano duplicati
* (non il numero totale di occorrenze, ma il numero di oggetti distinti che
* hanno almeno un duplicato)
*/
public static <T> Map<T, Integer> ContaDuplicati(BinaryNode<T> node) {
}
protected static <T> Map<T, Integer> ContaDuplicati(BinaryNode<T> node, HashMap<T, Integer> mappa) {
if (node == null) return mappa;
if (node.getLeft() != null)
}
}
@@ -0,0 +1,51 @@
package list.mylinkedlist;
import java.util.List;
public class MyLinkedList<E> implements List<E> {
/*
* Classe Nodo
*/
private static class Node<E> {
// Dati del nodo
private E data;
// Riferimenti elemento precedente e successivo
private Node<E> prev;
private Node<E> next;
// Costruttore
public Node(E data, Node<E> prev, Node<E> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
// Costruttore
public MyLinkedList() {}
// Metodi
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public E getFirst() {
if (head == null) throw new NullPointerException();
return head.data;
}
}
@@ -1,483 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder(node.getLeft(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
inorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
inorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE POSTORDER
/*
* Qui si utilizza il metodo postorder.
* Le regole per il postorder sono queste:
* - Visita prima di tutto ciò che si trova a sinistra
* - Poi visita tutto ciò che si trova a destra
* - Infine visita il sestesso
*/
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
postorder(node.getLeft(), temporaryList);
postorder(node.getRight(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
}
public Iterator<E> iteratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
postorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE LEVEL ORDER
/*
* Qui si usa il metodo levelorder.
* Si analizza tutto il livello dell'albero.
* Per chiarire di seguito un esempio:
*
* A
* / \
* B C
* / \ \
* D E F
*
* La lettura avviene quindi in questo modo:
* A - B - C - D - E - F
*
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
*/
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while(!queueOfNodes.isEmpty()) {
current = queueOfNodes.remove();
if (current.getData() != null) temporaryList.add(current.getData());
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
if (current.getRight() != null) queueOfNodes.add(current.getRight());
}
}
public Iterator<E> iteratorLevelOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
levelorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE
public Iterator<E> iterator() {
return iteratorPreOrder();
}
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso stiamo usando un tipo stack.
* Dobbiamo di fatto simulare uno stack se vogliamo
* iterativamente fare questa cosa.
*
* Perchè se prima dobbiamo visitare il nodo centrale,
* poi il nodo a sinistra e poi il nodo a destra inseriamo
* prima quello di destra e poi quello di sinistra?
*
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
* e quindi l'ultimo che entra è il primo che esce.
*
* dal momento che vale questa cosa, se facciamo entrare prima quello
* di destra e poi quello di sinistra, il primo nodo che verrà
* prelevato è ovviamente quello a sinistra.
*/
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
if (current.getData() != null) temporaryList.add(current.getData());
// Come già spiegato pushamo prima il nodo di destra
if (current.getRight() != null) queueOfNodes.push(current.getRight());
// Infine aggiungiamo il nodo di sinistra
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
}
}
public Iterator<E> ititeratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpreorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
/*
* Questo metodo riguarda lo stesso di quello superiore.
* Il metodo inorder richiede che si visiti prima sinistra,
* poi il nodo stesso, infine il nodo di destra.
*
* Possiamo quindi sempre adoperare uno Stack per siumlare la
* ricorsione.
*
* Come detto prima, se il primo nodo ad essere prelevato è
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
* sinistra -> centro -> destra.
*
* Nel metodo sono state inserite delle check con uno stack di
* flag che permette di capire quando il nodo centrale è visitare
* oppure no.
*/
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.add(node);
flags.push(false);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
if (current.getData() != null) temporaryList.add(current.getData());
} else {
// il nodo non è da visitare
// prima si aggiunge il nodo di destra
if (current.getRight() != null) {
queueOfNodes.add(current.getRight());
flags.add(false);
}
queueOfNodes.push(current);
flags.push(true);
if (current.getLeft() != null) {
queueOfNodes.add(current.getLeft());
flags.push(false);
}
}
}
}
public Iterator<E> ititeratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itinorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR POST ORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso la logica è la seguente. si ha la
* necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE
*
* Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario,
* dunque NODO CORRENTE -> DESTRA -> SINISTRA
*
* Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra.
*/
protected void itpostorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.push(node);
flags.push(false);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
if (current.getData() != null) temporaryList.add(current.getData());
} else {
// il nodo non è ancora da visitare
// Visitiamo il nodo corrente
queueOfNodes.push(current);
flags.push(true);
// visitiamo il nodo di destra
if (current.getRight() != null) {
queueOfNodes.push(current.getRight());
flags.push(false);
}
// visitiamo il nodo di sinistra infine
if (current.getLeft() != null) {
queueOfNodes.push(current.getLeft());
flags.push(false);
}
}
}
}
public Iterator<E> ititeratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpostorder(root, temporaryList);
return temporaryList.iterator();
}
/*
* II parziale 2022/2023
* Realizzare un metodo costruttore della classe LinkedBinaryTree<E> che
* prende in input una lista di oggetti di tipo E e costruisce una catena
* casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo
* della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro.
*/
/*
* Spiegazione intuitiva (visto che la melideo intende complicare la consegna):
* Data ad esempio una lista del genere:
* [A, B, C, D]
*
* Si ottiene quindi una catena del genere:
* A
* \
* B
* /
* C
* \
* D
*/
public LinkedBinaryTree(List<E> objectList) {
if (objectList == null) throw new NullPointerException();
Iterator<E> iterator = objectList.iterator();
if (!iterator.hasNext()) return;
E currentObject = iterator.next();
root = new BinaryNode<E>(currentObject);
BinaryNode<E> currentNode = root;
currentNode.setData(currentObject);
size = 1;
while (iterator.hasNext()) {
currentObject = iterator.next();
int wing = (int) (Math.random() * 2);
BinaryNode<E> newNode = new BinaryNode<E>(currentObject);
if (wing == 0) {
// Left
newNode.setParentAsLeftChild(currentNode);
} else {
// Right
newNode.setParentAsRightChild(currentNode);
}
currentNode = newNode;
size++;
}
}
/*
* II parziale 2022
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo
* che stampa le foglie dall'albero corrente (da sinistra verso destra)
*/
public void printLeaf() {
printLeaf(root);
}
public void printLeaf(BinaryNode<E> node) {
if (node == null) return;
if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString());
else {
printLeaf(node.getLeft());
printLeaf(node.getRight());
}
}
/*
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo che conta il numero
* di foglie dell'albero corrente
*/
public int numberLeaf() {
Integer number = 0;
numberLeaf(root, number);
return number;
}
public int numberLeaf(BinaryNode<E> node) {
}
}
@@ -0,0 +1,96 @@
package list.mylinkedlist;
import java.util.List;
import java.util.NoSuchElementException;
public class MyLinkedList<E> implements List<E> {
/*
* Classe Nodo
*/
private static class Node<E> {
// Dati del nodo
private E data;
// Riferimenti elemento precedente e successivo
private Node<E> prev;
private Node<E> next;
// Costruttore
public Node(E data, Node<E> prev, Node<E> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
// Costruttore
public MyLinkedList() {}
// Metodi
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public E getFirst() {
if (head == null) throw new NullPointerException();
return head.data;
}
public void addFirst(E item) {
if (item == null) throw new NullPointerException();
if (size == 0) {
head = tail = new Node<E>(item, null, null);
} else {
Node<E> newNode = new Node<E>(item, null, null);
head.prev = newNode;
newNode.next = head;
head = newNode;
}
size++;
}
public E removeFirst() {
if (head == null) throw new NoSuchElementException();
E tmp = head.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> firstPlace = head;
Node<E> secondPlace = head.next;
head = secondPlace;
secondPlace.prev = null;
firstPlace.next = null;
}
size--;
return tmp;
}
public E getLast() {
if (tail == null) throw new NullPointerException();
return tail.data;
}
public E removeLast() {
if (tail == null) throw new NoSuchElementException();
E tmp = tail.data;
if (size == 1) {
} else {
}
}
}
@@ -1,5 +0,0 @@
package jcf_set.exercise;
public class Insiemistica {
}
@@ -1,79 +0,0 @@
package binary_tree;
public class BinaryNode<E> {
// VARIABILI
private BinaryNode<E> left, right, parent;
private E data;
// COSTRUTTORI
public BinaryNode(E data) {
this.data = data;
left = right = parent = null;
}
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
this.data = data;
this.left = left;
this.right = right;
this.parent = null;
if (this.left != null) this.left.parent = this;
if (this.right != null) this.right.parent = this;
}
// METODI
public E getData() {
return data;
}
public BinaryNode<E> getLeft() {
return left;
}
public BinaryNode<E> getRight() {
return right;
}
public BinaryNode<E> getParent() {
return parent;
}
public boolean isRoot() {
return parent == null;
}
public boolean isLeftChild() {
return parent != null && parent.getLeft() == this;
}
public boolean isRightChild() {
return parent != null && parent.getRight() == this;
}
public void setData(E data) {
this.data = data;
}
public void setLeft(BinaryNode<E> left) {
this.left = left;
if (this.left != null) this.left.parent = this;
}
public void setRight(BinaryNode<E> right) {
this.right = right;
if (this.right != null) this.right.parent = this;
}
public BinaryNode<E> setParentAsLeftChild(BinaryNode<E> parent) {
if (this.parent == null) return null;
this.parent = parent;
}
}
@@ -0,0 +1,85 @@
package list.mylinkedlist;
import java.util.List;
import java.util.NoSuchElementException;
public class MyLinkedList<E> implements List<E> {
/*
* Classe Nodo
*/
private static class Node<E> {
// Dati del nodo
private E data;
// Riferimenti elemento precedente e successivo
private Node<E> prev;
private Node<E> next;
// Costruttore
public Node(E data, Node<E> prev, Node<E> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
// Costruttore
public MyLinkedList() {}
// Metodi
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public E getFirst() {
if (head == null) throw new NullPointerException();
return head.data;
}
public void addFirst(E item) {
if (item == null) throw new NullPointerException();
if (size == 0) {
head = tail = new Node<E>(item, null, null);
} else {
Node<E> newNode = new Node<E>(item, null, null);
head.prev = newNode;
newNode.next = head;
head = newNode;
}
size++;
}
public E removeFirst() {
if (head == null) throw new NoSuchElementException();
E tmp = head.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> firstPlace = head;
Node<E> secondPlace = head.next;
head = secondPlace;
secondPlace.prev = null;
firstPlace.next = null;
}
size--;
return tmp;
}
public E getLast() {
if (tail == null) throw new NullPointerException();
}
}
@@ -0,0 +1,24 @@
package jcf_map.exercise;
import java.util.TreeMap;
public class CompanyMap {
/*
* EX1
* Supponiamo che chi vengano forniti il nome e il numero
* di divisione di ciascun dipendente di un'azienda.
* Non ci sono nomi duplicati. Vorremmo memorizzare
* queste informazioni in ordine alfabetico per nome.
*/
/*
* Spiegazione: si intende adoperare chiaramente un TreeMap.
* In modo che vi sia una associazione chiave-valore e un ordinamento basato
* sulla chiave.
* Chiave: String
* Valore: Integer
*/
TreeMap<>
}
@@ -1,73 +0,0 @@
package binary_tree;
public class BinaryNode<E> {
// VARIABILI
private BinaryNode<E> left, right, parent;
private E data;
// COSTRUTTORI
public BinaryNode(E data) {
this.data = data;
left = right = parent = null;
}
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
this.data = data;
this.left = left;
this.right = right;
this.parent = null;
if (this.left != null) this.left.parent = this;
if (this.right != null) this.right.parent = this;
}
// METODI
public E getData() {
return data;
}
public BinaryNode<E> getLeft() {
return left;
}
public BinaryNode<E> getRight() {
return right;
}
public BinaryNode<E> getParent() {
return parent;
}
public boolean isRoot() {
return parent == null;
}
public boolean isLeftChild() {
return parent != null && parent.getLeft() == this;
}
public boolean isRightChild() {
return parent != null && parent.getRight() == this;
}
public void setData(E data) {
this.data = data;
}
public void setLeft(BinaryNode<E> left) {
this.left = left;
if (this.left != null) this.left.parent = this;
}
public void setRight() {
}
}
@@ -1,35 +0,0 @@
package jcf_set.exercise;
import java.util.ArrayList;
import java.util.List;
public class IteratoreSenzaDuplicati {
public static void main(String[] main) {
new IteratoreSenzaDuplicati().run();
}
public void run() {
List<String> lista = new ArrayList<String>();
lista.add("tree");
lista.add("flower");
lista.add("tree");
lista.add("flower");
lista.add("animal");
lista.add("flower");
lista.add("fruit");
}
// Esercizio 1
/*
* Ottenere una lista con duplicati.
* Ottenere un iteratore privo di duplicati.
*/
private static <T>
}
@@ -0,0 +1,73 @@
package list.mylinkedlist;
import java.util.List;
import java.util.NoSuchElementException;
public class MyLinkedList<E> implements List<E> {
/*
* Classe Nodo
*/
private static class Node<E> {
// Dati del nodo
private E data;
// Riferimenti elemento precedente e successivo
private Node<E> prev;
private Node<E> next;
// Costruttore
public Node(E data, Node<E> prev, Node<E> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
// Costruttore
public MyLinkedList() {}
// Metodi
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public E getFirst() {
if (head == null) throw new NullPointerException();
return head.data;
}
public void addFirst(E item) {
if (item == null) throw new NullPointerException();
if (size == 0) {
head = tail = new Node<E>(item, null, null);
} else {
Node<E> newNode = new Node<E>(item, null, null);
head.prev = newNode;
newNode.next = head;
head = newNode;
}
size++;
}
public E removeFirst() {
if (head == null) throw new NoSuchElementException();
if (size == 1) {
E tmp = head.data;
head = tail = null;
return tmp;
}
}
}
@@ -1,327 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder(node.getLeft(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
inorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
inorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE POSTORDER
/*
* Qui si utilizza il metodo postorder.
* Le regole per il postorder sono queste:
* - Visita prima di tutto ciò che si trova a sinistra
* - Poi visita tutto ciò che si trova a destra
* - Infine visita il sestesso
*/
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
postorder(node.getLeft(), temporaryList);
postorder(node.getRight(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
}
public Iterator<E> iteratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
postorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE LEVEL ORDER
/*
* Qui si usa il metodo levelorder.
* Si analizza tutto il livello dell'albero.
* Per chiarire di seguito un esempio:
*
* A
* / \
* B C
* / \ \
* D E F
*
* La lettura avviene quindi in questo modo:
* A - B - C - D - E - F
*
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
*/
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while(!queueOfNodes.isEmpty()) {
current = queueOfNodes.remove();
if (current.getData() != null) temporaryList.add(current.getData());
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
if (current.getRight() != null) queueOfNodes.add(current.getRight());
}
}
public Iterator<E> iteratorLevelOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
levelorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE
public Iterator<E> iterator() {
return iteratorPreOrder();
}
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso stiamo usando un tipo stack.
* Dobbiamo di fatto simulare uno stack se vogliamo
* iterativamente fare questa cosa.
*
* Perchè se prima dobbiamo visitare il nodo centrale,
* poi il nodo a sinistra e poi il nodo a destra inseriamo
* prima quello di destra e poi quello di sinistra?
*
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
* e quindi l'ultimo che entra è il primo che esce.
*
* dal momento che vale questa cosa, se facciamo entrare prima quello
* di destra e poi quello di sinistra, il primo nodo che verrà
* prelevato è ovviamente quello a sinistra.
*/
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
if (current.getData() != null) temporaryList.add(current.getData());
// Come già spiegato pushamo prima il nodo di destra
if (current.getRight() != null) queueOfNodes.push(current.getRight());
// Infine aggiungiamo il nodo di sinistra
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
}
}
public Iterator<E> ititeratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpreorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
/*
* Questo metodo riguarda lo stesso di quello superiore.
* Il metodo inorder richiede che si visiti prima sinistra,
* poi il nodo stesso, infine il nodo di destra.
*
* Possiamo quindi sempre adoperare uno Stack per siumlare la
* ricorsione.
*
* Come detto prima, se il primo nodo ad essere prelevato è
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
* sinistra -> centro -> destra, il primo nodo da inserire è quello di
* destra
*/
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.add(node);
flags.push(false);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
if (current.getData() != null) temporaryList.add(current.getData());
} else {
// il nodo non è da visitare
// prima si aggiunge il nodo di destra
if (current.getRight() != null) {
queueOfNodes.add(current.getRight());
flags.add(false);
}
stackOfNodes.push(current);
}
}
}
}
@@ -1,138 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.List;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder();
}
}
@@ -1,38 +0,0 @@
package jcf_set.exercise;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IteratoreSenzaDuplicati {
public static void main(String[] main) {
new IteratoreSenzaDuplicati().run();
}
public void run() {
List<String> lista = new ArrayList<String>();
lista.add("tree");
lista.add("flower");
lista.add("tree");
lista.add("flower");
lista.add("animal");
lista.add("flower");
lista.add("fruit");
}
// Esercizio 1
/*
* Ottenere una lista con duplicati.
* Ottenere un iteratore privo di duplicati.
*/
private static <T> Iterator<T> getIteratorNoDuplicates(List<T> ) {
}
}
@@ -0,0 +1,173 @@
package list.mylinkedlist;
import java.util.List;
import java.util.NoSuchElementException;
public class MyLinkedList<E> implements List<E> {
/*
* Classe Nodo
*/
private static class Node<E> {
// Dati del nodo
private E data;
// Riferimenti elemento precedente e successivo
private Node<E> prev;
private Node<E> next;
// Costruttore
public Node(E data, Node<E> prev, Node<E> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
// Costruttore
public MyLinkedList() {}
// Metodi
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public E getFirst() {
if (head == null) throw new NullPointerException();
return head.data;
}
public void addFirst(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
head.prev = newNode;
newNode.next = head;
head = newNode;
}
size++;
}
public E removeFirst() {
if (head == null) throw new NoSuchElementException();
E tmp = head.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> firstPlace = head;
Node<E> secondPlace = head.next;
head = secondPlace;
secondPlace.prev = null;
firstPlace.next = null;
}
size--;
return tmp;
}
public E getLast() {
if (tail == null) throw new NullPointerException();
return tail.data;
}
public E removeLast() {
if (tail == null) throw new NoSuchElementException();
E tmp = tail.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> lastNode = tail;
Node<E> penultimateNode = tail.prev;
tail = penultimateNode;
penultimateNode.next = null;
lastNode.prev = null;
}
size--;
return tmp;
}
public void addLast(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
Node<E> oldLastNode = tail;
oldLastNode.next = newNode;
newNode.prev = oldLastNode;
tail = newNode;
}
size++;
}
public void clear() {
head = tail = null;
size = 0;
}
public boolean add(E item) {
if (item == null) throw new NoSuchElementException();
addLast(item);
return true;
}
public void add(int index, E item) {
if (item == null) throw new NullPointerException();
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
// Se l'inserimento è richiesto nella prima posizione
if (index == 0) {
addFirst(item);
return;
}
// Se l'inserimento è richiesto nell'ultima posizione
if (index == size) {
addLast(item);
return;
}
// Se l'inserimento è richiesto nel generico posto i
Node<E> newNode = new Node<E>(item, null, null);
Node<E> prevNode = head;
for (int i = 0; i < index - 1; i++) {
prevNode = prevNode.next;
}
Node<E> nextNode = prevNode.next;
prevNode.next = newNode;
newNode.prev = prevNode;
newNode.next = nextNode;
nextNode.prev = newNode;
size++;
}
public E get(int index) {
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
Node<E> currentNode = head;
for (int i = 0; i < index; i++) {
currentNode = currentNode.next;
}
return currentNode.data;
}
public E set() {
}
}
@@ -0,0 +1,42 @@
package parziale.p191108;
import java.util.HashMap;
import java.util.Map;
public class Archivio {
// Variabili di instanza
private Map<String, Cliente> archivio = new HashMap<String, Cliente>();
// Metodi
// 2. Inserimento nell'archivio
/*
* Inserimento nell'archio attuale di una nuova associazione,
* dati la targa dell'automobile(String) e un riferimento al proprietario
* della classe Cliente: se l'automobile è già presente, si aggiorni l'informazione
* relativa al proprietario
*/
public void aggiungi(String targa, Cliente cliente) {
if (targa == null || cliente == null) throw new NullPointerException();
targa = targa.trim();
if (targa.isEmpty()) throw new IllegalArgumentException();
archivio.put(targa, cliente);
}
// 3. Cancellazione dall'archivio attuale di un'automobile, data la sua targa
public void remove(String targa) {
if (targa == null) throw new NullPointerException();
targa.trim();
if (targa.isEmpty()) throw new IllegalArgumentException();
archivio.remove(targa);
}
// 4. Creazione di un nuovo archivio contenete le automobili selezionate dall'archivio principale aventi il proprietario residente in un'altra città.
public Map<String, Paziente>
}
@@ -0,0 +1,34 @@
package jcf_map.exercise;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import binary_tree.BinaryNode;
public class BinaryTreeMap {
/*
* Esercizio 1 - Esame del secondo parziale 8/1/2019
* Scrivere un metodo generico statico ContaDuplicati che conta il numero
* di oggetti duplicati (non univoci) contenuti in un albero binario.
* Il risultato è il conteggio totale degli elementi che risultano duplicati
* (non il numero totale di occorrenze, ma il numero di oggetti distinti che
* hanno almeno un duplicato)
*/
public static <T> Map<T, Integer> ContaDuplicati(BinaryNode<T> node) {
}
protected static <T> Map<T, Integer> ContaDuplicati(BinaryNode<T> node, TreeMap<T, Integer> mappa) {
if (node == null) return mappa;
if (node.getLeft() != null) ContaDuplicati(node.getLeft(), mappa);
if (node.getRight() != null) ContaDuplicati(node.getRight(), mappa);
if (node.getData() == null) return mappa;
T data = node.getData();
if (mappa.get(data) == null) mappa.put();
}
}
@@ -1,84 +0,0 @@
package binary_tree;
public class BinaryNode<E> {
// VARIABILI
private BinaryNode<E> left, right, parent;
private E data;
// COSTRUTTORI
public BinaryNode(E data) {
this.data = data;
left = right = parent = null;
}
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
this.data = data;
this.left = left;
this.right = right;
this.parent = null;
if (this.left != null) this.left.parent = this;
if (this.right != null) this.right.parent = this;
}
// METODI
public E getData() {
return data;
}
public BinaryNode<E> getLeft() {
return left;
}
public BinaryNode<E> getRight() {
return right;
}
public BinaryNode<E> getParent() {
return parent;
}
public boolean isRoot() {
return parent == null;
}
public boolean isLeftChild() {
return parent != null && parent.getLeft() == this;
}
public boolean isRightChild() {
return parent != null && parent.getRight() == this;
}
public void setData(E data) {
this.data = data;
}
public void setLeft(BinaryNode<E> left) {
this.left = left;
if (this.left != null) this.left.parent = this;
}
public void setRight(BinaryNode<E> right) {
this.right = right;
if (this.right != null) this.right.parent = this;
}
public BinaryNode<E> setParentAsLeftChild(BinaryNode<E> parent) {
if (parent == null) return null;
this.parent = parent;
BinaryNode<E> oldLeft = this.parent.left;
this.parent.left = this;
return oldLeft;
}
}
@@ -1,248 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder(node.getLeft(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
inorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
inorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE POSTORDER
/*
* Qui si utilizza il metodo postorder.
* Le regole per il postorder sono queste:
* - Visita prima di tutto ciò che si trova a sinistra
* - Poi visita tutto ciò che si trova a destra
* - Infine visita il sestesso
*/
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
postorder(node.getLeft(), temporaryList);
postorder(node.getRight(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
}
public Iterator<E> iteratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
postorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE LEVEL ORDER
/*
* Qui si usa il metodo levelorder.
* Si analizza tutto il livello dell'albero.
* Per chiarire di seguito un esempio:
*
* A
* / \
* B C
* / \ \
* D E F
*
* La lettura avviene quindi in questo modo:
* A - B - C - D - E - F
*
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
*/
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while(!queueOfNodes.isEmpty()) {
current = queueOfNodes.remove();
if (current.getData() != null) temporaryList.add(current.getData());
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
if (current.getRight() != null) queueOfNodes.add(current.getRight());
}
}
public Iterator<E> iteratorLevelOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
levelorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE
public Iterator<E> iterator() {
return iteratorPreOrder();
}
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
protected void itpreorder(BinaryNode<E> node, List<E> temporaryList) {
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
}
}
@@ -1,62 +0,0 @@
package binary_tree;
public class BinaryNode<E> {
// VARIABILI
private BinaryNode<E> left, right, parent;
private E data;
// COSTRUTTORI
public BinaryNode(E data) {
this.data = data;
left = right = parent = null;
}
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
this.data = data;
this.left = left;
this.right = right;
this.parent = null;
if (this.left != null) this.left.parent = this;
if (this.right != null) this.right.parent = this;
}
// METODI
public E getData() {
return data;
}
public BinaryNode<E> getLeft() {
return left;
}
public BinaryNode<E> getRight() {
return right;
}
public BinaryNode<E> getParent() {
return parent;
}
public boolean isRoot() {
return parent == null;
}
public boolean isLeftChild() {
return parent != null && parent.getLeft() == this;
}
public boolean isRightChild() {
return parent != null && parent.getRight() == this;
}
}
@@ -0,0 +1,144 @@
package list.mylinkedlist;
import java.util.List;
import java.util.NoSuchElementException;
public class MyLinkedList<E> implements List<E> {
/*
* Classe Nodo
*/
private static class Node<E> {
// Dati del nodo
private E data;
// Riferimenti elemento precedente e successivo
private Node<E> prev;
private Node<E> next;
// Costruttore
public Node(E data, Node<E> prev, Node<E> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
// Costruttore
public MyLinkedList() {}
// Metodi
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public E getFirst() {
if (head == null) throw new NullPointerException();
return head.data;
}
public void addFirst(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
head.prev = newNode;
newNode.next = head;
head = newNode;
}
size++;
}
public E removeFirst() {
if (head == null) throw new NoSuchElementException();
E tmp = head.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> firstPlace = head;
Node<E> secondPlace = head.next;
head = secondPlace;
secondPlace.prev = null;
firstPlace.next = null;
}
size--;
return tmp;
}
public E getLast() {
if (tail == null) throw new NullPointerException();
return tail.data;
}
public E removeLast() {
if (tail == null) throw new NoSuchElementException();
E tmp = tail.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> lastNode = tail;
Node<E> penultimateNode = tail.prev;
tail = penultimateNode;
penultimateNode.next = null;
lastNode.prev = null;
}
size--;
return tmp;
}
public void addLast(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
Node<E> oldLastNode = tail;
oldLastNode.next = newNode;
newNode.prev = oldLastNode;
tail = newNode;
}
size++;
}
public void clear() {
head = tail = null;
size = 0;
}
public boolean add(E item) {
if (item == null) throw new NoSuchElementException();
addLast(item);
return true;
}
public void add(int index, E item) {
if (item == null) throw new NullPointerException();
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
if (index == 0) {
addFirst(item);
return;
}
if (index == size) {
addLast(item);
return;
}
}
}
@@ -1,9 +0,0 @@
package test;
public class Main {
public static void main (String[] Args) {
}
}
@@ -1,106 +0,0 @@
package binary_tree;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
}
return false;
}
}
@@ -0,0 +1,28 @@
package list.mylinkedlist;
import java.util.List;
public class MyLinkedList<E> implements List<E> {
/*
* Classe Nodo
*/
private static class Node<E> {
// Dati del nodo
private E data;
// Riferimenti elemento precedente e successivo
private Node<E> prev;
private Node<E> next;
// Costruttore
public Node(E data, Node<E> prev, Node<E> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
}
@@ -1,128 +0,0 @@
package binary_tree;
public class BinaryNode<E> {
// VARIABILI
private BinaryNode<E> left, right, parent;
private E data;
// COSTRUTTORI
public BinaryNode(E data) {
this.data = data;
left = right = parent = null;
}
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
this.data = data;
this.left = left;
this.right = right;
this.parent = null;
if (this.left != null) this.left.parent = this;
if (this.right != null) this.right.parent = this;
}
// METODI
public E getData() {
return data;
}
public BinaryNode<E> getLeft() {
return left;
}
public BinaryNode<E> getRight() {
return right;
}
public BinaryNode<E> getParent() {
return parent;
}
public boolean isRoot() {
return parent == null;
}
public boolean isLeftChild() {
return parent != null && parent.getLeft() == this;
}
public boolean isRightChild() {
return parent != null && parent.getRight() == this;
}
public void setData(E data) {
this.data = data;
}
public void setLeft(BinaryNode<E> left) {
this.left = left;
if (this.left != null) this.left.parent = this;
}
public void setRight(BinaryNode<E> right) {
this.right = right;
if (this.right != null) this.right.parent = this;
}
public BinaryNode<E> setParentAsLeftChild(BinaryNode<E> parent) {
if (parent == null) return null;
this.parent = parent;
BinaryNode<E> oldLeft = this.parent.left;
this.parent.left = this;
return oldLeft;
}
public BinaryNode<E> setParentAsRightChild(BinaryNode<E> parent) {
if (parent == null) return null;
this.parent = parent;
BinaryNode<E> oldRight = this.parent.right;
this.parent.right = this;
return oldRight;
}
public BinaryNode<E> setAsRoot() {
if (parent == null) return null;
BinaryNode<E> oldParent = parent;
parent = null;
if (oldParent.left == this) oldParent.left = null;
else oldParent.right = null;
return oldParent;
}
public boolean hasLeft() {
return left != null;
}
public boolean hasRight() {
return right != null;
}
@Override
public boolean equals(Object other) {
if (other == null) return false;
if (other == this) return true;
if (!(other instanceof BinaryNode<?>)) return false;
BinaryNode<?> otherType = (BinaryNode<?>)other;
return this.left.equals(otherType.left)
&& this.right.equals(otherType.right)
&& this.parent.equals(otherType.parent)
&& this.data.equals(otherType.data);
}
}
@@ -1,281 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder(node.getLeft(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
inorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
inorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE POSTORDER
/*
* Qui si utilizza il metodo postorder.
* Le regole per il postorder sono queste:
* - Visita prima di tutto ciò che si trova a sinistra
* - Poi visita tutto ciò che si trova a destra
* - Infine visita il sestesso
*/
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
postorder(node.getLeft(), temporaryList);
postorder(node.getRight(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
}
public Iterator<E> iteratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
postorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE LEVEL ORDER
/*
* Qui si usa il metodo levelorder.
* Si analizza tutto il livello dell'albero.
* Per chiarire di seguito un esempio:
*
* A
* / \
* B C
* / \ \
* D E F
*
* La lettura avviene quindi in questo modo:
* A - B - C - D - E - F
*
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
*/
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while(!queueOfNodes.isEmpty()) {
current = queueOfNodes.remove();
if (current.getData() != null) temporaryList.add(current.getData());
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
if (current.getRight() != null) queueOfNodes.add(current.getRight());
}
}
public Iterator<E> iteratorLevelOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
levelorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE
public Iterator<E> iterator() {
return iteratorPreOrder();
}
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso stiamo usando un tipo stack.
* Dobbiamo di fatto simulare uno stack se vogliamo
* iterativamente fare questa cosa.
*
* Perchè se prima dobbiamo visitare il nodo centrale,
* poi il nodo a sinistra e poi il nodo a destra inseriamo
* prima quello di destra e poi quello di sinistra?
*
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
* e quindi l'ultimo che entra è il primo che esce.
*
* dal momento che vale questa cosa, se facciamo entrare prima quello
* di destra e poi quello di sinistra, il primo nodo che verrà
* prelevato è ovviamente quello a sinistra.
*/
protected void itpreorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
if (current.getData() != null) temporaryList.add(current.getData());
// Come già spiegato pushamo prima il nodo di destra
if (current.getRight() != null) queueOfNodes.push(current.getRight());
// Infine aggiungiamo il nodo di sinistra
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
}
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpreorder(root, temporaryList);
}
// ITERATOR
}
@@ -0,0 +1,39 @@
package parziale.p191108;
import java.util.Objects;
public class Cliente {
// Variabili di instanza
private String nominativo;
private String cf;
private String cittaResidenza;
// Costruttore
public Cliente(
String nominativo,
String cf,
String cittaResidenza) {
this.nominativo = nominativo;
this.cf = cf;
this.cittaResidenza = cittaResidenza;
}
// Getters
public String nominativo() {
return nominativo;
}
public String cf() {
return cf;
}
public String cittaResidenza() {
return cittaResidenza;
}
// Hash
public int hashCode() {
return Objects.hash(nominativo, cf, cittaResidenza);
}
}
@@ -1,96 +0,0 @@
package binary_tree;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null)
}
}
@@ -0,0 +1,69 @@
package parziale.p191108;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
public class Archivio {
// Variabili di instanza
private Map<String, Cliente> archivio = new HashMap<String, Cliente>();
// Metodi
// 2. Inserimento nell'archivio
/*
* Inserimento nell'archio attuale di una nuova associazione,
* dati la targa dell'automobile(String) e un riferimento al proprietario
* della classe Cliente: se l'automobile è già presente, si aggiorni l'informazione
* relativa al proprietario
*/
public void aggiungi(String targa, Cliente cliente) {
if (targa == null || cliente == null) throw new NullPointerException();
targa = targa.trim();
if (targa.isEmpty()) throw new IllegalArgumentException();
archivio.put(targa, cliente);
}
// 3. Cancellazione dall'archivio attuale di un'automobile, data la sua targa
public void remove(String targa) {
if (targa == null) throw new NullPointerException();
targa.trim();
if (targa.isEmpty()) throw new IllegalArgumentException();
archivio.remove(targa);
}
// 4. Creazione di un nuovo archivio contenete le automobili selezionate dall'archivio principale aventi il proprietario residente in una data città.
public Map<String, Cliente> creaArchivoPerCitta(String citta) {
if (citta == null) throw new NullPointerException();
HashMap<String, Cliente> autoSelezionate = new HashMap<String, Cliente>();
for (String s : archivio.keySet()) {
Cliente currentCliente = archivio.get(s);
String currentCitta = currentCliente.cittaResidenza();
if (currentCitta.equals(citta)) autoSelezionate.put(s, currentCliente);
}
return autoSelezionate;
}
// 5. Ordinamento dellarchivio in modo lessicografico crescente rispetto alla targa delle automobili
public Map<String, Cliente> archivioOrdinatoRispettoAllaTarga() {
TreeMap<String, Cliente> archivioOrdinato = new TreeMap<String, Cliente>(archivio);
return archivioOrdinato;
}
// 6. Stampa su video l'archivio attuale
public void stampaArchivio() {
}
}
@@ -1,132 +0,0 @@
package binary_tree;
import java.util.List;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
}
@@ -0,0 +1,43 @@
package jcf_map.exercise;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
public class Dispari<E> {
/*
* Esercizio 1 - Esame del 5 settembre 2017
*
* Una lista si dice dispari se ciascuno dei suoi oggetti appare un numero dispari di volte.
* Ad esempio, la lista di interi [20, 5, 5, 20, 20] non è dispari, mentre lo è [5, 20, 5 , 5].
* Si assuma una classe Dispari<E> con una sola variabile di instanza private LinkedList<E> myList.
* Si implementi un metodo public boolean ordinaDispari() {} interno alla classe,
* che restituisce false se myLisy non è dispari, mentre ordina in modo crescente la lista
* e restituisce true se myList è dispari;
* Si assuma che gli elementi presenti nella lista implementino l'interfaccia Comparable<T>
*/
// Variabile di instanza
private LinkedList<E> myList = new LinkedList<E>();
// Metodo di esercizio
public boolean isDispari() {
boolean dispari = true;
// Se la lista è vuota allora returna dispari
if (myList.isEmpty()) return dispari;
// Contatore di elementi nella lista
HashMap<E, Integer> valori = new HashMap<E, Integer>();
Iterator<E> it = myList.iterator();
while (it.hasNext()) {
E current = it.next();
if (valori.get(current) == null) valori.put(current, 1);
else valori.put(current, valori.get(current) + 1);
}
}
}
@@ -1,60 +0,0 @@
package binary_tree;
public class BinaryNode<E> {
// VARIABILI
private BinaryNode<E> left, right, parent;
private E data;
// COSTRUTTORI
public BinaryNode(E data) {
this.data = data;
left = right = parent = null;
}
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
this.data = data;
this.left = left;
this.right = right;
this.parent = null;
if (this.left != null) this.left.parent = this;
if (this.right != null) this.right.parent = this;
}
// METODI
public E getData() {
return data;
}
public BinaryNode<E> getLeft() {
return left;
}
public BinaryNode<E> getRight() {
return right;
}
public BinaryNode<E> getParent() {
return parent;
}
public boolean isRoot() {
return parent == null;
}
public boolean isLeftChild() {
return parent != null && parent.getLeft() == this;
}
public boolean isRightChild() {
return parent != null && parent.getRight() == this;
}
}
@@ -0,0 +1,16 @@
package jcf_map.exercise;
public class CompanyMap {
/*
* EX1
* Supponiamo che chi vengano forniti il nome e il numero
* di divisione di ciascun dipendente di un'azienda.
* Non ci sono nomi duplicati. Vorremmo memorizzare
* queste informazioni in ordine alfabetico per nome.
*/
/*
* Spiegazione:
*/
}
@@ -0,0 +1,66 @@
package parziale.p191108;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
public class Archivio {
// Variabili di instanza
private Map<String, Cliente> archivio = new HashMap<String, Cliente>();
// Metodi
// 2. Inserimento nell'archivio
/*
* Inserimento nell'archio attuale di una nuova associazione,
* dati la targa dell'automobile(String) e un riferimento al proprietario
* della classe Cliente: se l'automobile è già presente, si aggiorni l'informazione
* relativa al proprietario
*/
public void aggiungi(String targa, Cliente cliente) {
if (targa == null || cliente == null) throw new NullPointerException();
targa = targa.trim();
if (targa.isEmpty()) throw new IllegalArgumentException();
archivio.put(targa, cliente);
}
// 3. Cancellazione dall'archivio attuale di un'automobile, data la sua targa
public void remove(String targa) {
if (targa == null) throw new NullPointerException();
targa.trim();
if (targa.isEmpty()) throw new IllegalArgumentException();
archivio.remove(targa);
}
// 4. Creazione di un nuovo archivio contenete le automobili selezionate dall'archivio principale aventi il proprietario residente in una data città.
public Map<String, Cliente> creaArchivoPerCitta(String citta) {
if (citta == null) throw new NullPointerException();
HashMap<String, Cliente> autoSelezionate = new HashMap<String, Cliente>();
for (String s : archivio.keySet()) {
Cliente currentCliente = archivio.get(s);
String currentCitta = currentCliente.cittaResidenza();
if (currentCitta.equals(citta)) autoSelezionate.put(s, currentCliente);
}
return autoSelezionate;
}
// 5. Ordinamento dellarchivio in modo lessicografico crescente rispetto alla targa delle automobili
public Map<String, Cliente> archivioOrdinatoRispettoAllaTarga() {
TreeMap<String, Cliente> archivioOrdinato = new TreeMap<String, Cliente>(new Comparator()<String> {
@Override
});
}
}
@@ -1,121 +0,0 @@
package binary_tree;
public class BinaryNode<E> {
// VARIABILI
private BinaryNode<E> left, right, parent;
private E data;
// COSTRUTTORI
public BinaryNode(E data) {
this.data = data;
left = right = parent = null;
}
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
this.data = data;
this.left = left;
this.right = right;
this.parent = null;
if (this.left != null) this.left.parent = this;
if (this.right != null) this.right.parent = this;
}
// METODI
public E getData() {
return data;
}
public BinaryNode<E> getLeft() {
return left;
}
public BinaryNode<E> getRight() {
return right;
}
public BinaryNode<E> getParent() {
return parent;
}
public boolean isRoot() {
return parent == null;
}
public boolean isLeftChild() {
return parent != null && parent.getLeft() == this;
}
public boolean isRightChild() {
return parent != null && parent.getRight() == this;
}
public void setData(E data) {
this.data = data;
}
public void setLeft(BinaryNode<E> left) {
this.left = left;
if (this.left != null) this.left.parent = this;
}
public void setRight(BinaryNode<E> right) {
this.right = right;
if (this.right != null) this.right.parent = this;
}
public BinaryNode<E> setParentAsLeftChild(BinaryNode<E> parent) {
if (parent == null) return null;
this.parent = parent;
BinaryNode<E> oldLeft = this.parent.left;
this.parent.left = this;
return oldLeft;
}
public BinaryNode<E> setParentAsRightChild(BinaryNode<E> parent) {
if (parent == null) return null;
this.parent = parent;
BinaryNode<E> oldRight = this.parent.right;
this.parent.right = this;
return oldRight;
}
public BinaryNode<E> setAsRoot() {
if (parent == null) return null;
BinaryNode<E> oldParent = parent;
parent = null;
if (oldParent.left == this) oldParent.left = null;
else oldParent.right = null;
return oldParent;
}
public boolean hasLeft() {
return left != null;
}
public boolean hasRight() {
return right != null;
}
@Override
public boolean equals(Object other) {
if (other == null) return false;
if (other == this) return true;
if (!(other instanceof BinaryNode<?>))
}
}
@@ -1,193 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder(node.getLeft(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
inorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
inorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE POSTORDER
/*
* Qui si utilizza il metodo postorder.
* Le regole per il postorder sono queste:
* - Visita prima di tutto ciò che si trova a sinistra
* - Poi visita tutto ciò che si trova a destra
* - Infine visita il sestesso
*/
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
postorder(node.getLeft(), temporaryList);
postorder(node.getRight(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
}
public Iterator<E> iteratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
postorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE LEVEL ORDER
}
@@ -1,101 +0,0 @@
package binary_tree;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
}
}
@@ -1,94 +0,0 @@
package binary_tree;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
}
}
@@ -1,31 +0,0 @@
package binary_tree;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
}
// METODI
@Override
public boolean isEmpty() {
}
}
@@ -1,28 +0,0 @@
package jcf_set.exercise;
import java.util.ArrayList;
import java.util.List;
public class IteratoreSenzaDuplicati {
public static void main(String[] main) {
new IteratoreSenzaDuplicati().run();
}
public void run() {
List<String> lista = new ArrayList<String>();
lista.add("tree");
lista.add("flower");
lista.add("tree");
lista.add("flower");
lista.add("animal");
lista.add("flower");
lista.add("fruit");
}
}
@@ -1,432 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder(node.getLeft(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
inorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
inorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE POSTORDER
/*
* Qui si utilizza il metodo postorder.
* Le regole per il postorder sono queste:
* - Visita prima di tutto ciò che si trova a sinistra
* - Poi visita tutto ciò che si trova a destra
* - Infine visita il sestesso
*/
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
postorder(node.getLeft(), temporaryList);
postorder(node.getRight(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
}
public Iterator<E> iteratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
postorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE LEVEL ORDER
/*
* Qui si usa il metodo levelorder.
* Si analizza tutto il livello dell'albero.
* Per chiarire di seguito un esempio:
*
* A
* / \
* B C
* / \ \
* D E F
*
* La lettura avviene quindi in questo modo:
* A - B - C - D - E - F
*
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
*/
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while(!queueOfNodes.isEmpty()) {
current = queueOfNodes.remove();
if (current.getData() != null) temporaryList.add(current.getData());
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
if (current.getRight() != null) queueOfNodes.add(current.getRight());
}
}
public Iterator<E> iteratorLevelOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
levelorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE
public Iterator<E> iterator() {
return iteratorPreOrder();
}
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso stiamo usando un tipo stack.
* Dobbiamo di fatto simulare uno stack se vogliamo
* iterativamente fare questa cosa.
*
* Perchè se prima dobbiamo visitare il nodo centrale,
* poi il nodo a sinistra e poi il nodo a destra inseriamo
* prima quello di destra e poi quello di sinistra?
*
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
* e quindi l'ultimo che entra è il primo che esce.
*
* dal momento che vale questa cosa, se facciamo entrare prima quello
* di destra e poi quello di sinistra, il primo nodo che verrà
* prelevato è ovviamente quello a sinistra.
*/
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
if (current.getData() != null) temporaryList.add(current.getData());
// Come già spiegato pushamo prima il nodo di destra
if (current.getRight() != null) queueOfNodes.push(current.getRight());
// Infine aggiungiamo il nodo di sinistra
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
}
}
public Iterator<E> ititeratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpreorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
/*
* Questo metodo riguarda lo stesso di quello superiore.
* Il metodo inorder richiede che si visiti prima sinistra,
* poi il nodo stesso, infine il nodo di destra.
*
* Possiamo quindi sempre adoperare uno Stack per siumlare la
* ricorsione.
*
* Come detto prima, se il primo nodo ad essere prelevato è
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
* sinistra -> centro -> destra.
*
* Nel metodo sono state inserite delle check con uno stack di
* flag che permette di capire quando il nodo centrale è visitare
* oppure no.
*/
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.add(node);
flags.push(false);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
if (current.getData() != null) temporaryList.add(current.getData());
} else {
// il nodo non è da visitare
// prima si aggiunge il nodo di destra
if (current.getRight() != null) {
queueOfNodes.add(current.getRight());
flags.add(false);
}
queueOfNodes.push(current);
flags.push(true);
if (current.getLeft() != null) {
queueOfNodes.add(current.getLeft());
flags.push(false);
}
}
}
}
public Iterator<E> ititeratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itinorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR POST ORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso la logica è la seguente. si ha la
* necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE
*
* Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario,
* dunque NODO CORRENTE -> DESTRA -> SINISTRA
*
* Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra.
*/
protected void itpostorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.push(node);
flags.push(false);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
if (current.getData() != null) temporaryList.add(current.getData());
} else {
// il nodo non è ancora da visitare
// Visitiamo il nodo corrente
queueOfNodes.push(current);
flags.push(true);
// visitiamo il nodo di destra
if (current.getRight() != null) {
queueOfNodes.push(current.getRight());
flags.push(false);
}
// visitiamo il nodo di sinistra infine
if (current.getLeft() != null) {
queueOfNodes.push(current.getLeft());
flags.push(false);
}
}
}
}
public Iterator<E> ititeratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpostorder(root, temporaryList);
return temporaryList.iterator();
}
/*
* II parziale 2022/2023
* Realizzare un metodo costruttore della classe LinkedBinaryTree<E> che
* prende in input una lista di oggetti di tipo E e costruisce una catena
* casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo
* della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro.
*/
/*
* Spiegazione intuitiva (visto che la melideo intende complicare la consegna):
* Data ad esempio una lista del genere:
* [A, B, C, D]
*
* Si ottiene quindi una catena del genere:
* A
* \
* B
* /
* C
* \
* D
*/
public LinkedBinaryTree(List<E> objectList) {
if (objectList == null) throw new NullPointerException();
Iterator<E> iterator = objectList.iterator();
BinaryNode<E> currentNode = root;
while (iterator.hasNext()) {
E currentObject = iterator.next();
currentNode.setData(currentObject);
}
}
}
@@ -1,13 +0,0 @@
package jcf_set.exercise;
public class IteratoreSenzaDuplicati {
public static void main(String[] main) {
new IteratoreSenzaDuplicati().run();
}
public void run() {
}
}
@@ -0,0 +1,134 @@
package list.mylinkedlist;
import java.util.List;
import java.util.NoSuchElementException;
public class MyLinkedList<E> implements List<E> {
/*
* Classe Nodo
*/
private static class Node<E> {
// Dati del nodo
private E data;
// Riferimenti elemento precedente e successivo
private Node<E> prev;
private Node<E> next;
// Costruttore
public Node(E data, Node<E> prev, Node<E> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
// Costruttore
public MyLinkedList() {}
// Metodi
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public E getFirst() {
if (head == null) throw new NullPointerException();
return head.data;
}
public void addFirst(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
head.prev = newNode;
newNode.next = head;
head = newNode;
}
size++;
}
public E removeFirst() {
if (head == null) throw new NoSuchElementException();
E tmp = head.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> firstPlace = head;
Node<E> secondPlace = head.next;
head = secondPlace;
secondPlace.prev = null;
firstPlace.next = null;
}
size--;
return tmp;
}
public E getLast() {
if (tail == null) throw new NullPointerException();
return tail.data;
}
public E removeLast() {
if (tail == null) throw new NoSuchElementException();
E tmp = tail.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> lastNode = tail;
Node<E> penultimateNode = tail.prev;
tail = penultimateNode;
penultimateNode.next = null;
lastNode.prev = null;
}
size--;
return tmp;
}
public void addLast(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
Node<E> oldLastNode = tail;
oldLastNode.next = newNode;
newNode.prev = oldLastNode;
tail = newNode;
}
size++;
}
public void clear() {
head = tail = null;
size = 0;
}
public boolean add(E item) {
if (item == null) throw new NoSuchElementException();
addLast(item);
return true;
}
public void add(int index, E item) {
if (item == null) throw new NullPointerException();
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
}
}
@@ -0,0 +1,50 @@
package jcf_map.exercise;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
public class Dispari<E extends Comparable<? super E>> {
/*
* Esercizio 1 - Esame del 5 settembre 2017
*
* Una lista si dice dispari se ciascuno dei suoi oggetti appare un numero dispari di volte.
* Ad esempio, la lista di interi [20, 5, 5, 20, 20] non è dispari, mentre lo è [5, 20, 5 , 5].
* Si assuma una classe Dispari<E> con una sola variabile di instanza private LinkedList<E> myList.
* Si implementi un metodo public boolean ordinaDispari() {} interno alla classe,
* che restituisce false se myLisy non è dispari, mentre ordina in modo crescente la lista
* e restituisce true se myList è dispari;
* Si assuma che gli elementi presenti nella lista implementino l'interfaccia Comparable<T>
*/
// Variabile di instanza
private LinkedList<E> myList = new LinkedList<E>();
// Metodo di esercizio
public boolean isDispari() {
// Se la lista è vuota allora returna dispari
if (myList.isEmpty()) return true;
// Contatore di elementi nella lista
HashMap<E, Integer> valori = new HashMap<E, Integer>();
Iterator<E> it = myList.iterator();
while (it.hasNext()) {
E current = it.next();
if (valori.get(current) == null) valori.put(current, 1);
else valori.put(current, valori.get(current) + 1);
}
// Controllo short circuit di disparità
for (E chiave : valori.keySet()) {
int valore = valori.get(chiave);
if (valore % 2 == 0) return false;
}
// Arrivati qui la lista è dispari, manca solo l'ordinamento.
Collections.sort(myList);
return true;
}
}
@@ -0,0 +1,26 @@
package jcf_map.exercise;
import java.util.LinkedList;
public class Dispari<E> {
/*
* Esercizio 1 - Esame del 5 settembre 2017
*
* Una lista si dice dispari se ciascuno dei suoi oggetti appare un numero dispari di volte.
* Ad esempio, la lista di interi [20, 5, 5, 20, 20] non è dispari, mentre lo è [5, 20, 5 , 5].
* Si assuma una classe Dispari<E> con una sola variabile di instanza private LinkedList<E> myList.
* Si implementi un metodo public boolean ordinaDispari() {} interno alla classe,
* che restituisce false se myLisy non è dispari, mentre ordina in modo crescente la lista
* e restituisce true se myList è dispari;
* Si assuma che gli elementi presenti nella lista implementino l'interfaccia Comparable<T>
*/
// Variabile di instanza
private LinkedList<E> myList = new LinkedList<E>();
// Metodo di esercizio
public boolean isDispari() {
// Verifica che la lista si pari o dispari
}
}
@@ -1,224 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder(node.getLeft(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
inorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
inorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE POSTORDER
/*
* Qui si utilizza il metodo postorder.
* Le regole per il postorder sono queste:
* - Visita prima di tutto ciò che si trova a sinistra
* - Poi visita tutto ciò che si trova a destra
* - Infine visita il sestesso
*/
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
postorder(node.getLeft(), temporaryList);
postorder(node.getRight(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
}
public Iterator<E> iteratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
postorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE LEVEL ORDER
/*
* Qui si usa il metodo levelorder.
* Si analizza tutto il livello dell'albero.
* Per chiarire di seguito un esempio:
*
* A
* / \
* B C
* / \ \
* D E F
*
* La lettura avviene quindi in questo modo:
* A - B - C - D - E - F
*
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
*/
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while(!queueOfNodes.isEmpty()) {
current = queueOfNodes.remove();
if (current.getData() != null) templist.add();
}
}
}
@@ -1,383 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder(node.getLeft(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
inorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
inorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE POSTORDER
/*
* Qui si utilizza il metodo postorder.
* Le regole per il postorder sono queste:
* - Visita prima di tutto ciò che si trova a sinistra
* - Poi visita tutto ciò che si trova a destra
* - Infine visita il sestesso
*/
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
postorder(node.getLeft(), temporaryList);
postorder(node.getRight(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
}
public Iterator<E> iteratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
postorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE LEVEL ORDER
/*
* Qui si usa il metodo levelorder.
* Si analizza tutto il livello dell'albero.
* Per chiarire di seguito un esempio:
*
* A
* / \
* B C
* / \ \
* D E F
*
* La lettura avviene quindi in questo modo:
* A - B - C - D - E - F
*
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
*/
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while(!queueOfNodes.isEmpty()) {
current = queueOfNodes.remove();
if (current.getData() != null) temporaryList.add(current.getData());
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
if (current.getRight() != null) queueOfNodes.add(current.getRight());
}
}
public Iterator<E> iteratorLevelOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
levelorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE
public Iterator<E> iterator() {
return iteratorPreOrder();
}
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso stiamo usando un tipo stack.
* Dobbiamo di fatto simulare uno stack se vogliamo
* iterativamente fare questa cosa.
*
* Perchè se prima dobbiamo visitare il nodo centrale,
* poi il nodo a sinistra e poi il nodo a destra inseriamo
* prima quello di destra e poi quello di sinistra?
*
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
* e quindi l'ultimo che entra è il primo che esce.
*
* dal momento che vale questa cosa, se facciamo entrare prima quello
* di destra e poi quello di sinistra, il primo nodo che verrà
* prelevato è ovviamente quello a sinistra.
*/
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
if (current.getData() != null) temporaryList.add(current.getData());
// Come già spiegato pushamo prima il nodo di destra
if (current.getRight() != null) queueOfNodes.push(current.getRight());
// Infine aggiungiamo il nodo di sinistra
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
}
}
public Iterator<E> ititeratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpreorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
/*
* Questo metodo riguarda lo stesso di quello superiore.
* Il metodo inorder richiede che si visiti prima sinistra,
* poi il nodo stesso, infine il nodo di destra.
*
* Possiamo quindi sempre adoperare uno Stack per siumlare la
* ricorsione.
*
* Come detto prima, se il primo nodo ad essere prelevato è
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
* sinistra -> centro -> destra.
*
* Nel metodo sono state inserite delle check con uno stack di
* flag che permette di capire quando il nodo centrale è visitare
* oppure no.
*/
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.add(node);
flags.push(false);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
if (current.getData() != null) temporaryList.add(current.getData());
} else {
// il nodo non è da visitare
// prima si aggiunge il nodo di destra
if (current.getRight() != null) {
queueOfNodes.add(current.getRight());
flags.add(false);
}
queueOfNodes.push(current);
flags.push(true);
if (current.getLeft() != null) {
queueOfNodes.add(current.getLeft());
flags.push(false);
}
}
}
}
public Iterator<E> ititeratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itinorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR POST ORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso la logica è la seguente. si ha la
* necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE
*
* Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario,
* dunque NODO CORRENTE -> DESTRA -> SINISTRA
*
* Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra.
*/
protected void itpostorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.push(node);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
if (current.getData() != null) temporaryList.add(current.getData());
} else {
// il nodo non è ancora da visitare
// Visitiamo il nodo corrente
queueOfNodes.push(current);
flags.push(true);
// visitiamo il nodo di destra
if (current.getRight() != null) {
queueOfNodes.push(current.getRight());
}
}
}
}
}
@@ -1,83 +0,0 @@
package binary_tree;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
}
}
@@ -0,0 +1,69 @@
package parziale.p191108;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
public class Archivio {
// Variabili di instanza
private Map<String, Cliente> archivio = new HashMap<String, Cliente>();
// Metodi
// 2. Inserimento nell'archivio
/*
* Inserimento nell'archio attuale di una nuova associazione,
* dati la targa dell'automobile(String) e un riferimento al proprietario
* della classe Cliente: se l'automobile è già presente, si aggiorni l'informazione
* relativa al proprietario
*/
public void aggiungi(String targa, Cliente cliente) {
if (targa == null || cliente == null) throw new NullPointerException();
targa = targa.trim();
if (targa.isEmpty()) throw new IllegalArgumentException();
archivio.put(targa, cliente);
}
// 3. Cancellazione dall'archivio attuale di un'automobile, data la sua targa
public void remove(String targa) {
if (targa == null) throw new NullPointerException();
targa.trim();
if (targa.isEmpty()) throw new IllegalArgumentException();
archivio.remove(targa);
}
// 4. Creazione di un nuovo archivio contenete le automobili selezionate dall'archivio principale aventi il proprietario residente in una data città.
public Map<String, Cliente> creaArchivoPerCitta(String citta) {
if (citta == null) throw new NullPointerException();
HashMap<String, Cliente> autoSelezionate = new HashMap<String, Cliente>();
for (String s : archivio.keySet()) {
Cliente currentCliente = archivio.get(s);
String currentCitta = currentCliente.cittaResidenza();
if (currentCitta.equals(citta)) autoSelezionate.put(s, currentCliente);
}
return autoSelezionate;
}
// 5. Ordinamento dellarchivio in modo lessicografico crescente rispetto alla targa delle automobili
public Map<String, Cliente> archivioOrdinatoRispettoAllaTarga() {
TreeMap<String, Cliente> archivioOrdinato = new TreeMap<String, Cliente>(archivio);
return archivioOrdinato;
}
// 6. Stampa su video l'archivio attuale
public void stampaArchivio() {
}
}
@@ -0,0 +1,142 @@
package list.mylinkedlist;
import java.util.List;
import java.util.NoSuchElementException;
public class MyLinkedList<E> implements List<E> {
/*
* Classe Nodo
*/
private static class Node<E> {
// Dati del nodo
private E data;
// Riferimenti elemento precedente e successivo
private Node<E> prev;
private Node<E> next;
// Costruttore
public Node(E data, Node<E> prev, Node<E> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
// Costruttore
public MyLinkedList() {}
// Metodi
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public E getFirst() {
if (head == null) throw new NullPointerException();
return head.data;
}
public void addFirst(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
head.prev = newNode;
newNode.next = head;
head = newNode;
}
size++;
}
public E removeFirst() {
if (head == null) throw new NoSuchElementException();
E tmp = head.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> firstPlace = head;
Node<E> secondPlace = head.next;
head = secondPlace;
secondPlace.prev = null;
firstPlace.next = null;
}
size--;
return tmp;
}
public E getLast() {
if (tail == null) throw new NullPointerException();
return tail.data;
}
public E removeLast() {
if (tail == null) throw new NoSuchElementException();
E tmp = tail.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> lastNode = tail;
Node<E> penultimateNode = tail.prev;
tail = penultimateNode;
penultimateNode.next = null;
lastNode.prev = null;
}
size--;
return tmp;
}
public void addLast(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
Node<E> oldLastNode = tail;
oldLastNode.next = newNode;
newNode.prev = oldLastNode;
tail = newNode;
}
size++;
}
public void clear() {
head = tail = null;
size = 0;
}
public boolean add(E item) {
if (item == null) throw new NoSuchElementException();
addLast(item);
return true;
}
public void add(int index, E item) {
if (item == null) throw new NullPointerException();
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
if (index == 0) {
addFirst(item);
return;
}
if (index == size) {
}
}
}
@@ -1,87 +0,0 @@
package binary_tree;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
size = size - rightTree.size;
return rightTree;
}
}
@@ -1,5 +0,0 @@
package test;
public class Main {
}
@@ -0,0 +1,115 @@
package list.mylinkedlist;
import java.util.List;
import java.util.NoSuchElementException;
public class MyLinkedList<E> implements List<E> {
/*
* Classe Nodo
*/
private static class Node<E> {
// Dati del nodo
private E data;
// Riferimenti elemento precedente e successivo
private Node<E> prev;
private Node<E> next;
// Costruttore
public Node(E data, Node<E> prev, Node<E> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
// Costruttore
public MyLinkedList() {}
// Metodi
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public E getFirst() {
if (head == null) throw new NullPointerException();
return head.data;
}
public void addFirst(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
head.prev = newNode;
newNode.next = head;
head = newNode;
}
size++;
}
public E removeFirst() {
if (head == null) throw new NoSuchElementException();
E tmp = head.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> firstPlace = head;
Node<E> secondPlace = head.next;
head = secondPlace;
secondPlace.prev = null;
firstPlace.next = null;
}
size--;
return tmp;
}
public E getLast() {
if (tail == null) throw new NullPointerException();
return tail.data;
}
public E removeLast() {
if (tail == null) throw new NoSuchElementException();
E tmp = tail.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> lastNode = tail;
Node<E> penultimateNode = tail.prev;
tail = penultimateNode;
penultimateNode.next = null;
lastNode.prev = null;
}
size--;
return tmp;
}
public void addLast(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
Node<E> oldLastNode = tail;
oldLastNode.next = newNode;
}
size++;
}
}
@@ -1,26 +0,0 @@
package binary_tree;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
}
// METODI
@Override
public boolean isEmpty() {
}
}
@@ -0,0 +1,148 @@
package list.mylinkedlist;
import java.util.List;
import java.util.NoSuchElementException;
public class MyLinkedList<E> implements List<E> {
/*
* Classe Nodo
*/
private static class Node<E> {
// Dati del nodo
private E data;
// Riferimenti elemento precedente e successivo
private Node<E> prev;
private Node<E> next;
// Costruttore
public Node(E data, Node<E> prev, Node<E> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
// Costruttore
public MyLinkedList() {}
// Metodi
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public E getFirst() {
if (head == null) throw new NullPointerException();
return head.data;
}
public void addFirst(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
head.prev = newNode;
newNode.next = head;
head = newNode;
}
size++;
}
public E removeFirst() {
if (head == null) throw new NoSuchElementException();
E tmp = head.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> firstPlace = head;
Node<E> secondPlace = head.next;
head = secondPlace;
secondPlace.prev = null;
firstPlace.next = null;
}
size--;
return tmp;
}
public E getLast() {
if (tail == null) throw new NullPointerException();
return tail.data;
}
public E removeLast() {
if (tail == null) throw new NoSuchElementException();
E tmp = tail.data;
if (size == 1) {
head = tail = null;
} else {
Node<E> lastNode = tail;
Node<E> penultimateNode = tail.prev;
tail = penultimateNode;
penultimateNode.next = null;
lastNode.prev = null;
}
size--;
return tmp;
}
public void addLast(E item) {
if (item == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(item, null, null);
if (size == 0) {
head = tail = newNode;
} else {
Node<E> oldLastNode = tail;
oldLastNode.next = newNode;
newNode.prev = oldLastNode;
tail = newNode;
}
size++;
}
public void clear() {
head = tail = null;
size = 0;
}
public boolean add(E item) {
if (item == null) throw new NoSuchElementException();
addLast(item);
return true;
}
public void add(int index, E item) {
if (item == null) throw new NullPointerException();
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
// Se l'inserimento è richiesto nella prima posizione
if (index == 0) {
addFirst(item);
return;
}
// Se l'inserimento è richiesto nell'ultima posizione
if (index == size) {
addLast(item);
return;
}
// Se l'inserimento è richiesto nel generico posto i
Node<E> newNode = new Node<E>(item, null, null);
Node<E> prevNode = head;
for (int i = 0; i < size; i++)
}
}
@@ -1,28 +0,0 @@
package vettore_ordinabile;
public class VettoriIntero extends VettoreOrdinabile{
// Metodo costruttore default
/*
* Contiene solamente 10 elementi
* */
public VettoriIntero() {
super(10);
}
// Metodo costruttore custom
/*
* Contiene n elementi
*/
public VettoriIntero(int dimensioneMassima) {
super(dimensioneMassima);
}
// Metodo aggiungi
/*
* Questo metodo è un metodo bloccante in modo che
* non aggiungino oggetti che non siano di tipo Integer
*/
public
}
@@ -1,156 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder();
}
}
@@ -0,0 +1,12 @@
package list.mylinkedlist;
import java.array.List;
public class MyLinkedList<E> implements List<E> {
/*
* Classe Nodo
*/
private static class Node<E>
}
@@ -1,445 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder(node.getLeft(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
inorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
inorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE POSTORDER
/*
* Qui si utilizza il metodo postorder.
* Le regole per il postorder sono queste:
* - Visita prima di tutto ciò che si trova a sinistra
* - Poi visita tutto ciò che si trova a destra
* - Infine visita il sestesso
*/
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
postorder(node.getLeft(), temporaryList);
postorder(node.getRight(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
}
public Iterator<E> iteratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
postorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE LEVEL ORDER
/*
* Qui si usa il metodo levelorder.
* Si analizza tutto il livello dell'albero.
* Per chiarire di seguito un esempio:
*
* A
* / \
* B C
* / \ \
* D E F
*
* La lettura avviene quindi in questo modo:
* A - B - C - D - E - F
*
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
*/
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while(!queueOfNodes.isEmpty()) {
current = queueOfNodes.remove();
if (current.getData() != null) temporaryList.add(current.getData());
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
if (current.getRight() != null) queueOfNodes.add(current.getRight());
}
}
public Iterator<E> iteratorLevelOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
levelorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE
public Iterator<E> iterator() {
return iteratorPreOrder();
}
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso stiamo usando un tipo stack.
* Dobbiamo di fatto simulare uno stack se vogliamo
* iterativamente fare questa cosa.
*
* Perchè se prima dobbiamo visitare il nodo centrale,
* poi il nodo a sinistra e poi il nodo a destra inseriamo
* prima quello di destra e poi quello di sinistra?
*
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
* e quindi l'ultimo che entra è il primo che esce.
*
* dal momento che vale questa cosa, se facciamo entrare prima quello
* di destra e poi quello di sinistra, il primo nodo che verrà
* prelevato è ovviamente quello a sinistra.
*/
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
if (current.getData() != null) temporaryList.add(current.getData());
// Come già spiegato pushamo prima il nodo di destra
if (current.getRight() != null) queueOfNodes.push(current.getRight());
// Infine aggiungiamo il nodo di sinistra
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
}
}
public Iterator<E> ititeratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpreorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
/*
* Questo metodo riguarda lo stesso di quello superiore.
* Il metodo inorder richiede che si visiti prima sinistra,
* poi il nodo stesso, infine il nodo di destra.
*
* Possiamo quindi sempre adoperare uno Stack per siumlare la
* ricorsione.
*
* Come detto prima, se il primo nodo ad essere prelevato è
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
* sinistra -> centro -> destra.
*
* Nel metodo sono state inserite delle check con uno stack di
* flag che permette di capire quando il nodo centrale è visitare
* oppure no.
*/
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.add(node);
flags.push(false);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
if (current.getData() != null) temporaryList.add(current.getData());
} else {
// il nodo non è da visitare
// prima si aggiunge il nodo di destra
if (current.getRight() != null) {
queueOfNodes.add(current.getRight());
flags.add(false);
}
queueOfNodes.push(current);
flags.push(true);
if (current.getLeft() != null) {
queueOfNodes.add(current.getLeft());
flags.push(false);
}
}
}
}
public Iterator<E> ititeratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itinorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR POST ORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso la logica è la seguente. si ha la
* necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE
*
* Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario,
* dunque NODO CORRENTE -> DESTRA -> SINISTRA
*
* Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra.
*/
protected void itpostorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.push(node);
flags.push(false);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
if (current.getData() != null) temporaryList.add(current.getData());
} else {
// il nodo non è ancora da visitare
// Visitiamo il nodo corrente
queueOfNodes.push(current);
flags.push(true);
// visitiamo il nodo di destra
if (current.getRight() != null) {
queueOfNodes.push(current.getRight());
flags.push(false);
}
// visitiamo il nodo di sinistra infine
if (current.getLeft() != null) {
queueOfNodes.push(current.getLeft());
flags.push(false);
}
}
}
}
public Iterator<E> ititeratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpostorder(root, temporaryList);
return temporaryList.iterator();
}
/*
* II parziale 2022/2023
* Realizzare un metodo costruttore della classe LinkedBinaryTree<E> che
* prende in input una lista di oggetti di tipo E e costruisce una catena
* casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo
* della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro.
*/
/*
* Spiegazione intuitiva (visto che la melideo intende complicare la consegna):
* Data ad esempio una lista del genere:
* [A, B, C, D]
*
* Si ottiene quindi una catena del genere:
* A
* \
* B
* /
* C
* \
* D
*/
public LinkedBinaryTree(List<E> objectList) {
if (objectList == null) throw new NullPointerException();
Iterator<E> iterator = objectList.iterator();
if (!iterator.hasNext()) return;
BinaryNode<E> currentNode = root;
E currentObject = iterator.next();
currentNode.setData(currentObject);
while (iterator.hasNext()) {
currentObject = iterator.next();
int wing = (int) (Math.random() * 2);
BinaryNode<E> newNode = new BinaryNode<E>(currentObject);
//newNode.setParent(currentNode);
if (wing == 0) {
// Left Wing
newNode.setParentAsLeftChild(currentNode);
} else {
// Right Wing
}
}
}
}
@@ -1,79 +0,0 @@
package vettore_ordinabile;
public abstract class VettoreOrdinabile {
// Variabili di istanza
private Object[] vettore; // Vettore dove memorizzare i dati
private int dimensioneMassima;
private int dimensioneCorrente;
// Costruttore
public VettoreOrdinabile(int dimensioneMassima) {
if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa");
this.dimensioneMassima = dimensioneMassima;
}
// FUNZIONI DI ISTANZA
/**
* Funzione aggiungi.
* Permette di aggiungere un elemento se la dimensione lo permette.
*/
public boolean aggiungi(Object elemento) {
if (elemento != null && dimensioneCorrente < dimensioneMassima) {
vettore[dimensioneCorrente] = elemento;
dimensioneCorrente++;
return true;
} else return false;
}
/**
* Funzione leggi.
* Permette di leggere un elemento ad una determinata posizione.
*/
public Object leggi(int indice) {
if (indice >= 0 && indice < dimensioneCorrente) {
return vettore[indice];
} else return null;
}
/**
* Funzione che restituisce la dimensione corrente
*/
public int dimensione() {
return dimensioneCorrente;
}
/**
* Funzione che visualizza l'array
*/
public void visualizza() {
this.ordina();
for (int i = 0; i < this.dimensioneMassima; i++) {
System.out.println(leggi(i));
}
}
/**
* Funzione che ordina l'array
*/
public void ordina() {
for (int i = 0; i < dimensioneCorrente; i++) {
int minimo = i;
int j;
for (j = i+1; j < dimensioneCorrente; j++) {
if (confronta(vettore[minimo], vettore[j]) > 0) minimo = j;
}
Object temp = vettore[minimo];
vettore[minimo] = vettore[j];
vettore[j] = temp;
}
}
// FUNZIONI ASTRATTE
/**
* Funzione che restituisce il valore della comparazione
*/
protected abstract int confronta(Object elemento1, Object elemento2);
}
@@ -1,103 +0,0 @@
package binary_tree;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
}
}
@@ -0,0 +1,37 @@
package jcf_map.exercise;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import binary_tree.BinaryNode;
public class BinaryTreeMap {
/*
* Esercizio 1 - Esame del secondo parziale 8/1/2019
* Scrivere un metodo generico statico ContaDuplicati che conta il numero
* di oggetti duplicati (non univoci) contenuti in un albero binario.
* Il risultato è il conteggio totale degli elementi che risultano duplicati
* (non il numero totale di occorrenze, ma il numero di oggetti distinti che
* hanno almeno un duplicato)
*/
public static <T> Map<T, Integer> ContaDuplicati(BinaryNode<T> node) {
TreeMap<T, Integer> mappa = new TreeMap<T, Integer>();
ContaDuplicati(node, mappa);
return mappa;
}
protected static <T> void ContaDuplicati(BinaryNode<T> node, TreeMap<T, Integer> mappa) {
if (node == null) return;
if (node.getLeft() != null) ContaDuplicati(node.getLeft(), mappa);
if (node.getRight() != null) ContaDuplicati(node.getRight(), mappa);
if (node.getData() == null) return;
T data = node.getData();
if (mappa.get(data) == null) mappa.put(data, 1);
else mappa.put(data, mappa.get(data) + 1);
}
}
@@ -0,0 +1,21 @@
package parziale.p191108;
public class Cliente {
// Variabili di instanza
private String nominativo;
private String cf;
private String cittaResidenza;
// Costruttore
public Cliente(
String nominativo,
String cf,
String cittaResidenza) {
if ()
this.nominativo = nominativo;
this.cf = cf;
this.cittaResidenza = cittaResidenza;
}
}
@@ -1,74 +0,0 @@
package binary_tree;
public class BinaryNode<E> {
// VARIABILI
private BinaryNode<E> left, right, parent;
private E data;
// COSTRUTTORI
public BinaryNode(E data) {
this.data = data;
left = right = parent = null;
}
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
this.data = data;
this.left = left;
this.right = right;
this.parent = null;
if (this.left != null) this.left.parent = this;
if (this.right != null) this.right.parent = this;
}
// METODI
public E getData() {
return data;
}
public BinaryNode<E> getLeft() {
return left;
}
public BinaryNode<E> getRight() {
return right;
}
public BinaryNode<E> getParent() {
return parent;
}
public boolean isRoot() {
return parent == null;
}
public boolean isLeftChild() {
return parent != null && parent.getLeft() == this;
}
public boolean isRightChild() {
return parent != null && parent.getRight() == this;
}
public void setData(E data) {
this.data = data;
}
public void setLeft(BinaryNode<E> left) {
this.left = left;
if (this.left != null) this.left.parent = this;
}
public void setRight(BinaryNode<E> right) {
this.right = right;
if (this.right != null)
}
}
@@ -1,324 +0,0 @@
package binary_tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
if (root == null) return null;
if (root.getData().equals(targetElement)) return root;
BinaryNode<E> resNode;
resNode = find(targetElement, root.getLeft());
if (resNode == null) resNode = find(targetElement, root.getRight());
return resNode;
}
public boolean remove(E targetElement) {
if (targetElement == null) return false;
BinaryNode<E> temp = find(targetElement, root);
if (temp != null) {
temp.setData(null);
return true;
}
return false;
}
public boolean contains(E targetElement) {
return find(targetElement, root) != null;
}
// ITERATORE PREORDER
/*
* Qui si utilizza il metodo del preorder.
* Le regole per il preorder sono queste:
* - Visita prima se stesso
* - Poi visita tutto ciò che si trova sinistra
* - Infine visita tutto ciò che si trova a destra
*/
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
if (node.getData() != null) temporaryList.add(node.getData());
preorder(node.getLeft(), temporaryList);
preorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorPreOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
preorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE INORDER
/*
* Qui si utilizza il metodo del inorder.
* Le regole per l'inorder sono queste:
* - Visita prima tutto ciò che si trova a sinistra
* - Poi visita se stesso
* - Infine visita tutto ciò che si trova a destra
*/
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
inorder(node.getLeft(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
inorder(node.getRight(), temporaryList);
}
public Iterator<E> iteratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
inorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE POSTORDER
/*
* Qui si utilizza il metodo postorder.
* Le regole per il postorder sono queste:
* - Visita prima di tutto ciò che si trova a sinistra
* - Poi visita tutto ciò che si trova a destra
* - Infine visita il sestesso
*/
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
if (node == null) return;
postorder(node.getLeft(), temporaryList);
postorder(node.getRight(), temporaryList);
if (node.getData() != null) temporaryList.add(node.getData());
}
public Iterator<E> iteratorPostOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
postorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE LEVEL ORDER
/*
* Qui si usa il metodo levelorder.
* Si analizza tutto il livello dell'albero.
* Per chiarire di seguito un esempio:
*
* A
* / \
* B C
* / \ \
* D E F
*
* La lettura avviene quindi in questo modo:
* A - B - C - D - E - F
*
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
*/
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while(!queueOfNodes.isEmpty()) {
current = queueOfNodes.remove();
if (current.getData() != null) temporaryList.add(current.getData());
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
if (current.getRight() != null) queueOfNodes.add(current.getRight());
}
}
public Iterator<E> iteratorLevelOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
levelorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATORE
public Iterator<E> iterator() {
return iteratorPreOrder();
}
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
/*
* In questo caso stiamo usando un tipo stack.
* Dobbiamo di fatto simulare uno stack se vogliamo
* iterativamente fare questa cosa.
*
* Perchè se prima dobbiamo visitare il nodo centrale,
* poi il nodo a sinistra e poi il nodo a destra inseriamo
* prima quello di destra e poi quello di sinistra?
*
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
* e quindi l'ultimo che entra è il primo che esce.
*
* dal momento che vale questa cosa, se facciamo entrare prima quello
* di destra e poi quello di sinistra, il primo nodo che verrà
* prelevato è ovviamente quello a sinistra.
*/
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
BinaryNode<E> current;
queueOfNodes.add(node);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
if (current.getData() != null) temporaryList.add(current.getData());
// Come già spiegato pushamo prima il nodo di destra
if (current.getRight() != null) queueOfNodes.push(current.getRight());
// Infine aggiungiamo il nodo di sinistra
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
}
}
public Iterator<E> ititeratorInOrder() {
ArrayList<E> temporaryList = new ArrayList<E>();
itpreorder(root, temporaryList);
return temporaryList.iterator();
}
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
/*
* Questo metodo riguarda lo stesso di quello superiore.
* Il metodo inorder richiede che si visiti prima sinistra,
* poi il nodo stesso, infine il nodo di destra.
*
* Possiamo quindi sempre adoperare uno Stack per siumlare la
* ricorsione.
*
* Come detto prima, se il primo nodo ad essere prelevato è
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
* sinistra -> centro -> destra, il primo nodo da inserire è quello di
* destra
*/
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
Stack<Boolean> flags = new Stack<Boolean>();
BinaryNode<E> current;
Boolean flag;
queueOfNodes.add(node);
flags.push(false);
while (!queueOfNodes.isEmpty()) {
current = queueOfNodes.pop();
flag = flags.pop();
if (flag) {
// il nodo è da visitare
if (current.getData() != null) temporaryList.add(current.getData());
} else {
// il nodo non è da visitare
// prima si aggiunge il nodo di destra
if (current.getRight() != null) {
queueOfNodes.add(current.getRight());
}
}
}
}
}
@@ -1,88 +0,0 @@
package binary_tree;
public class LinkedBinaryTree<E> implements BinaryTree<E>{
// VARIABILI D'INSTANZA
private BinaryNode<E> root;
private int size;
// Metodi costruttori
public LinkedBinaryTree() {
root = null;
size = 0;
}
public LinkedBinaryTree(E data) {
root = new BinaryNode<E>(data);
size = 1;
}
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
root = new BinaryNode<E>(left.root, data, right.root);
size = 1 + left.size + right.size;
}
// METODI
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return root == null;
}
public E getRoot() {
if (isEmpty()) return null;
return root.getData();
}
public BinaryNode<E> getRootNode() {
if (isEmpty()) return null;
return root;
}
public void clear() {
root = null;
size = 0;
}
private int getSize(BinaryNode<E> node) {
if (node == null) return 0;
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
return 1 + nLeft + nRight;
}
public LinkedBinaryTree<E> removeLeft() {
LinkedBinaryTree<E> leftTree = null;
if (root.getLeft() == null) return leftTree;
leftTree = new LinkedBinaryTree<E>();
leftTree.root = root.getLeft();
leftTree.size = getSize(root.getLeft());
leftTree.root.setAsRoot();
size = size - leftTree.size;
return leftTree;
}
public LinkedBinaryTree<E> removeRight() {
LinkedBinaryTree<E> rightTree = null;
if (root.getRight() == null) return rightTree;
rightTree = new LinkedBinaryTree<E>();
rightTree.root = root.getRight();
rightTree.size = getSize(root.getRight());
rightTree.root.setAsRoot();
size = size - rightTree.size;
return rightTree;
}
}
@@ -1,25 +0,0 @@
package binary_tree;
public class BinaryNode<E> {
// VARIABILI
private BinaryNode<E> left, right, parent;
private E data;
// COSTRUTTORI
public BinaryNode(E data) {
this.data = data;
left = right = parent = null;
}
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
this.data = data;
}
// METODI
}
@@ -1,7 +0,0 @@
package binary_tree;
public interface BinaryTree {
public boolean isEmpty();
}

Some files were not shown because too many files have changed in this diff Show More