diff --git a/.metadata/.lock_info b/.metadata/.lock_info index 3d76fad..694992f 100644 --- a/.metadata/.lock_info +++ b/.metadata/.lock_info @@ -1,4 +1,4 @@ -#Sun May 10 01:31:23 CEST 2026 +#Sun May 10 18:29:39 CEST 2026 host=DESKTOP-ALBQHEK -process-id=13140 +process-id=13748 user=eslusarz diff --git a/.metadata/.log b/.metadata/.log index c60ca44..d965545 100644 --- a/.metadata/.log +++ b/.metadata/.log @@ -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.JavaEditorTextHoverProxy.getHoverInfo2(JavaEditorTextHoverProxy.java:89) 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(); +----- + 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. diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/0/1041b5053b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/0/1041b5053b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..bc08294 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/0/1041b5053b4d0011135ca7e1c9885acd @@ -0,0 +1,150 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < size; i++) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/0/20930145034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/0/20930145034c00111d9cbe34e2b6027a deleted file mode 100644 index 692ba1b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/0/20930145034c00111d9cbe34e2b6027a +++ /dev/null @@ -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 lista = new ArrayList(); - - 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 Iterator getIteratorNoDuplicates(Iterator it) { - HashSet - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/0/70059da43e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/0/70059da43e490011162d85db97d6efcd deleted file mode 100644 index 08c1e94..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/0/70059da43e490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while () { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/0/900497d880460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/0/900497d880460011162d85db97d6efcd deleted file mode 100644 index 72f35b1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/0/900497d880460011162d85db97d6efcd +++ /dev/null @@ -1,31 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(); - } - - // METODI - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/0/b0f5b1bc40490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/0/b0f5b1bc40490011162d85db97d6efcd deleted file mode 100644 index 4cb4459..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/0/b0f5b1bc40490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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); - } - - - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/0/d003190040490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/0/d003190040490011162d85db97d6efcd deleted file mode 100644 index acd33d2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/0/d003190040490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/0/e00b44d82b470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/0/e00b44d82b470011162d85db97d6efcd deleted file mode 100644 index ab6a88a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/0/e00b44d82b470011162d85db97d6efcd +++ /dev/null @@ -1,92 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/0/e0c30c8f81460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/0/e0c30c8f81460011162d85db97d6efcd deleted file mode 100644 index db797a5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/0/e0c30c8f81460011162d85db97d6efcd +++ /dev/null @@ -1,40 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/0/f08464e1364d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/0/f08464e1364d0011135ca7e1c9885acd new file mode 100644 index 0000000..5f51121 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/0/f08464e1364d0011135ca7e1c9885acd @@ -0,0 +1,98 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/0095d2f77f460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1/0095d2f77f460011162d85db97d6efcd deleted file mode 100644 index 8991a01..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1/0095d2f77f460011162d85db97d6efcd +++ /dev/null @@ -1,16 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - - - // METODI - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/30e625e95c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1/30e625e95c490011162d85db97d6efcd deleted file mode 100644 index 74a8855..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1/30e625e95c490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - - } - - public void printLeaf(BinaryNode node) { - if (node == null) return; - - if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); - else { - printLeaf(node.getLeft()); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/4039dfcd81460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1/4039dfcd81460011162d85db97d6efcd deleted file mode 100644 index 69e8da3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1/4039dfcd81460011162d85db97d6efcd +++ /dev/null @@ -1,51 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/40e4ee163b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/1/40e4ee163b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..66f8596 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1/40e4ee163b4d0011135ca7e1c9885acd @@ -0,0 +1,150 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index; i++) { + prevNode = prevNode.next; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/a077e1602f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1/a077e1602f470011162d85db97d6efcd deleted file mode 100644 index c6b2a11..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1/a077e1602f470011162d85db97d6efcd +++ /dev/null @@ -1,129 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData != null) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/e003eb146a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1/e003eb146a460011162d85db97d6efcd deleted file mode 100644 index 584bb15..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1/e003eb146a460011162d85db97d6efcd +++ /dev/null @@ -1,78 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/10/10a789b53e4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/10/10a789b53e4600111c1cd5b6f02d115c deleted file mode 100644 index e7d3c62..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/10/10a789b53e4600111c1cd5b6f02d115c +++ /dev/null @@ -1,5 +0,0 @@ -package vettore_ordinabile; - -public class VettoriIntero { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/10/30987e3f034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/10/30987e3f034c00111d9cbe34e2b6027a deleted file mode 100644 index 090189a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/10/30987e3f034c00111d9cbe34e2b6027a +++ /dev/null @@ -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 lista = new ArrayList(); - - 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 Iterator getIteratorNoDuplicates(Iterator it) { - HashSet implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node nextNode = prevNode.next; + + prevNode.next = newNode; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/10/a0ab1ab53e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/10/a0ab1ab53e490011162d85db97d6efcd deleted file mode 100644 index be84efc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/10/a0ab1ab53e490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - if () - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/10/b0ff7b065a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/10/b0ff7b065a490011162d85db97d6efcd deleted file mode 100644 index 452ad5d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/10/b0ff7b065a490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - BinaryNode currentNode = root; - - while (iterator.hasNext()) { - E currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - currentNode.setData(currentObject); - - if (wing == 0) { - - } else { - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/11/100969af7f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/11/100969af7f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..f4a5f80 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/11/100969af7f4c00111d9cbe34e2b6027a @@ -0,0 +1,60 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeSet archivioOrdinato = new TreeSet(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/11/2043eb4e7d4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/11/2043eb4e7d4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..83618ee --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/11/2043eb4e7d4c00111d9cbe34e2b6027a @@ -0,0 +1,42 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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à. + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/11/a0a17875a94c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/11/a0a17875a94c0011135ca7e1c9885acd new file mode 100644 index 0000000..4cf2dc5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/11/a0a17875a94c0011135ca7e1c9885acd @@ -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. + */ +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/11/e02ffab06a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/11/e02ffab06a460011162d85db97d6efcd deleted file mode 100644 index 0678796..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/11/e02ffab06a460011162d85db97d6efcd +++ /dev/null @@ -1,91 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/12/5000980885460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/12/5000980885460011162d85db97d6efcd deleted file mode 100644 index 2bc745d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/12/5000980885460011162d85db97d6efcd +++ /dev/null @@ -1,68 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree.root = root.getLeft(); - return leftTree; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/12/80417c38094c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/12/80417c38094c00111d9cbe34e2b6027a deleted file mode 100644 index 0e1693b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/12/80417c38094c00111d9cbe34e2b6027a +++ /dev/null @@ -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 aHashSet = new HashSet(Arrays.asList(aArray)); - Set bHashSet = new HashSet(Arrays.asList(bArray)); - - - - } - - // Operazioni di insiemistica - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/12/9025886b084c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/12/9025886b084c00111d9cbe34e2b6027a deleted file mode 100644 index 532044f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/12/9025886b084c00111d9cbe34e2b6027a +++ /dev/null @@ -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}; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/12/904377c885460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/12/904377c885460011162d85db97d6efcd deleted file mode 100644 index c03bf87..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/12/904377c885460011162d85db97d6efcd +++ /dev/null @@ -1,74 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/12/a0c89fa35d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/12/a0c89fa35d490011162d85db97d6efcd deleted file mode 100644 index 99c0434..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/12/a0c89fa35d490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public void numberLeaf() { - Integer number = 0; - numberLeaf(root, number); - } - - public void numberLeaf(BinaryNode node, Integer number) { - if (node == null) return; - - if (node.getLeft() == null && node.getRight() == null) number++; - else { - numberLeaf(node.getLeft(), number); - numberLeaf(node.getRight(), number); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/12/e081ed3eaa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/12/e081ed3eaa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..f1fa49e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/12/e081ed3eaa4c0011135ca7e1c9885acd @@ -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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + if (node == null) return mappa; + + if (node.getLeft() != null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/13/2024ff882f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/13/2024ff882f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..ffdd3d5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/13/2024ff882f4d0011135ca7e1c9885acd @@ -0,0 +1,51 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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; + } + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/13/20c9da0a5e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/13/20c9da0a5e490011162d85db97d6efcd deleted file mode 100644 index ceb5f29..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/13/20c9da0a5e490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 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 node) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/13/40a97f1b364d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/13/40a97f1b364d0011135ca7e1c9885acd new file mode 100644 index 0000000..ca68c64 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/13/40a97f1b364d0011135ca7e1c9885acd @@ -0,0 +1,96 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/13/604a48b4054c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/13/604a48b4054c00111d9cbe34e2b6027a deleted file mode 100644 index 50917c4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/13/604a48b4054c00111d9cbe34e2b6027a +++ /dev/null @@ -1,5 +0,0 @@ -package jcf_set.exercise; - -public class Insiemistica { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/13/a0e694456a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/13/a0e694456a460011162d85db97d6efcd deleted file mode 100644 index 8f54683..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/13/a0e694456a460011162d85db97d6efcd +++ /dev/null @@ -1,79 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (this.parent == null) return null; - this.parent = parent; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/13/c0b854a3354d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/13/c0b854a3354d0011135ca7e1c9885acd new file mode 100644 index 0000000..bb22b37 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/13/c0b854a3354d0011135ca7e1c9885acd @@ -0,0 +1,85 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node secondPlace = head.next; + + head = secondPlace; + secondPlace.prev = null; + firstPlace.next = null; + } + size--; + return tmp; + } + + public E getLast() { + if (tail == null) throw new NullPointerException(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/13/e047d6461b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/13/e047d6461b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..909e501 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/13/e047d6461b4d0011135ca7e1c9885acd @@ -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<> + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/13/e0919e1569460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/13/e0919e1569460011162d85db97d6efcd deleted file mode 100644 index 03137e7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/13/e0919e1569460011162d85db97d6efcd +++ /dev/null @@ -1,73 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/13/f0eec60d034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/13/f0eec60d034c00111d9cbe34e2b6027a deleted file mode 100644 index a918377..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/13/f0eec60d034c00111d9cbe34e2b6027a +++ /dev/null @@ -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 lista = new ArrayList(); - - 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 - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/14/20c737b9324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/14/20c737b9324d0011135ca7e1c9885acd new file mode 100644 index 0000000..0d66c16 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/14/20c737b9324d0011135ca7e1c9885acd @@ -0,0 +1,73 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/14/706367cf40490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/14/706367cf40490011162d85db97d6efcd deleted file mode 100644 index 7215794..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/14/706367cf40490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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); - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/14/a05f2dd22f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/14/a05f2dd22f470011162d85db97d6efcd deleted file mode 100644 index 0fe706a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/14/a05f2dd22f470011162d85db97d6efcd +++ /dev/null @@ -1,138 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - preorder(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/14/b025a533034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/14/b025a533034c00111d9cbe34e2b6027a deleted file mode 100644 index 3f39b30..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/14/b025a533034c00111d9cbe34e2b6027a +++ /dev/null @@ -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 lista = new ArrayList(); - - 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 Iterator getIteratorNoDuplicates(List ) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/14/b0a053983c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/14/b0a053983c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..1e63fe8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/14/b0a053983c4d0011135ca7e1c9885acd @@ -0,0 +1,173 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + return currentNode.data; + } + + public E set() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/14/d084dd927d4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/14/d084dd927d4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..ace65f2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/14/d084dd927d4c00111d9cbe34e2b6027a @@ -0,0 +1,42 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/14/e0655db2ab4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/14/e0655db2ab4c0011135ca7e1c9885acd new file mode 100644 index 0000000..f6d3ddb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/14/e0655db2ab4c0011135ca7e1c9885acd @@ -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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, TreeMap 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(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/15/206cc4926a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/15/206cc4926a460011162d85db97d6efcd deleted file mode 100644 index 353fa04..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/15/206cc4926a460011162d85db97d6efcd +++ /dev/null @@ -1,84 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/15/505ac16132490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/15/505ac16132490011162d85db97d6efcd deleted file mode 100644 index 89b3429..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/15/505ac16132490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator iterator() { - return iteratorPreOrder(); - } - - // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) - - protected void itpreorder(BinaryNode node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode current; - - queueOfNodes.add(node); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/15/701630bb68460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/15/701630bb68460011162d85db97d6efcd deleted file mode 100644 index 100bbc2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/15/701630bb68460011162d85db97d6efcd +++ /dev/null @@ -1,62 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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; - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/15/d05528283a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/15/d05528283a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..51cd951 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/15/d05528283a4d0011135ca7e1c9885acd @@ -0,0 +1,144 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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; + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/15/f009323e2f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/15/f009323e2f4600111c1cd5b6f02d115c deleted file mode 100644 index 91d2140..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/15/f009323e2f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,9 +0,0 @@ -package test; - -public class Main { - - public static void main (String[] Args) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/15/f0914c102d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/15/f0914c102d470011162d85db97d6efcd deleted file mode 100644 index 4d4d0d3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/15/f0914c102d470011162d85db97d6efcd +++ /dev/null @@ -1,106 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - if (temp != null) { - - } - return false; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/16/40767bf61e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/16/40767bf61e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..648d536 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/16/40767bf61e4d0011135ca7e1c9885acd @@ -0,0 +1,28 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/16/60bcdb7175460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/16/60bcdb7175460011162d85db97d6efcd deleted file mode 100644 index 6607137..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/16/60bcdb7175460011162d85db97d6efcd +++ /dev/null @@ -1,128 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/16/a0b9858d3d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/16/a0b9858d3d490011162d85db97d6efcd deleted file mode 100644 index 2edba06..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/16/a0b9858d3d490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - itpreorder(root, temporaryList); - } - - // ITERATOR - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/16/c0e064237e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/16/c0e064237e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..9854512 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/16/c0e064237e4c00111d9cbe34e2b6027a @@ -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); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/17/103e05502c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/17/103e05502c470011162d85db97d6efcd deleted file mode 100644 index 096c319..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/17/103e05502c470011162d85db97d6efcd +++ /dev/null @@ -1,96 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode resNode; - resNode = find(targetElement, root.getLeft()); - if (resNode == null) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/17/b00c1050804c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/17/b00c1050804c00111d9cbe34e2b6027a new file mode 100644 index 0000000..a59dc54 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/17/b00c1050804c00111d9cbe34e2b6027a @@ -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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(archivio); + return archivioOrdinato; + } + + // 6. Stampa su video l'archivio attuale + public void stampaArchivio() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/17/b03944b82f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/17/b03944b82f470011162d85db97d6efcd deleted file mode 100644 index 0fd5323..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/17/b03944b82f470011162d85db97d6efcd +++ /dev/null @@ -1,132 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/17/d0dd87059e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/17/d0dd87059e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..d769c20 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/17/d0dd87059e4c0011135ca7e1c9885acd @@ -0,0 +1,43 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // 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 valori = new HashMap(); + + Iterator 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); + } + + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/17/e02b569268460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/17/e02b569268460011162d85db97d6efcd deleted file mode 100644 index dd9371f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/17/e02b569268460011162d85db97d6efcd +++ /dev/null @@ -1,60 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/18/d0b643071b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/18/d0b643071b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..1db9306 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/18/d0b643071b4d0011135ca7e1c9885acd @@ -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: + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/19/5081a3d07f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/19/5081a3d07f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..b9cf51b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/19/5081a3d07f4c00111d9cbe34e2b6027a @@ -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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(new Comparator() { + @Override + + }); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/19/604b22da73460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/19/604b22da73460011162d85db97d6efcd deleted file mode 100644 index 5ecd425..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/19/604b22da73460011162d85db97d6efcd +++ /dev/null @@ -1,121 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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)) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/19/6072390933470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/19/6072390933470011162d85db97d6efcd deleted file mode 100644 index 68b5dc6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/19/6072390933470011162d85db97d6efcd +++ /dev/null @@ -1,193 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - postorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE LEVEL ORDER - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/19/707b36db2c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/19/707b36db2c470011162d85db97d6efcd deleted file mode 100644 index bbf4c95..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/19/707b36db2c470011162d85db97d6efcd +++ /dev/null @@ -1,101 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/19/d052e6302c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/19/d052e6302c470011162d85db97d6efcd deleted file mode 100644 index 7ef93ec..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/19/d052e6302c470011162d85db97d6efcd +++ /dev/null @@ -1,94 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode resNode; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/19/f0d1eeb280460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/19/f0d1eeb280460011162d85db97d6efcd deleted file mode 100644 index 73b1af4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/19/f0d1eeb280460011162d85db97d6efcd +++ /dev/null @@ -1,31 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - - } - - // METODI - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1a/50ff51ef024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/1a/50ff51ef024c00111d9cbe34e2b6027a deleted file mode 100644 index 06394c9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1a/50ff51ef024c00111d9cbe34e2b6027a +++ /dev/null @@ -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 lista = new ArrayList(); - - lista.add("tree"); - lista.add("flower"); - lista.add("tree"); - lista.add("flower"); - lista.add("animal"); - lista.add("flower"); - lista.add("fruit"); - - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/103a19cf59490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/103a19cf59490011162d85db97d6efcd deleted file mode 100644 index 30adbac..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/103a19cf59490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - BinaryNode currentNode = root; - - while (iterator.hasNext()) { - E currentObject = iterator.next(); - - currentNode.setData(currentObject); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/305b39a9024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/305b39a9024c00111d9cbe34e2b6027a deleted file mode 100644 index 4d866d9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/305b39a9024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,13 +0,0 @@ -package jcf_set.exercise; - -public class IteratoreSenzaDuplicati { - - public static void main(String[] main) { - new IteratoreSenzaDuplicati().run(); - } - - public void run() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/3063929c394d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/3063929c394d0011135ca7e1c9885acd new file mode 100644 index 0000000..dccb934 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/3063929c394d0011135ca7e1c9885acd @@ -0,0 +1,134 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/8081d9eb9e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/8081d9eb9e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..b651a36 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/8081d9eb9e4c0011135ca7e1c9885acd @@ -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> { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/a0ab5e579b4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/a0ab5e579b4c0011135ca7e1c9885acd new file mode 100644 index 0000000..b078779 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/a0ab5e579b4c0011135ca7e1c9885acd @@ -0,0 +1,26 @@ +package jcf_map.exercise; + +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Verifica che la lista si pari o dispari + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/d00ddd7035470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/d00ddd7035470011162d85db97d6efcd deleted file mode 100644 index e6efac4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/d00ddd7035470011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode current; - - queueOfNodes.add(node); - while(!queueOfNodes.isEmpty()) { - current = queueOfNodes.remove(); - - if (current.getData() != null) templist.add(); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/f04eb43243490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/f04eb43243490011162d85db97d6efcd deleted file mode 100644 index 7a12f8f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/f04eb43243490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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()); - } - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/1089a0fc85460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/1089a0fc85460011162d85db97d6efcd deleted file mode 100644 index 163a607..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/1089a0fc85460011162d85db97d6efcd +++ /dev/null @@ -1,83 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/308fbd54804c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/308fbd54804c00111d9cbe34e2b6027a new file mode 100644 index 0000000..a59dc54 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/308fbd54804c00111d9cbe34e2b6027a @@ -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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(archivio); + return archivioOrdinato; + } + + // 6. Stampa su video l'archivio attuale + public void stampaArchivio() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/504dba233a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/504dba233a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..d80a225 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/504dba233a4d0011135ca7e1c9885acd @@ -0,0 +1,142 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/50fdff1086460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/50fdff1086460011162d85db97d6efcd deleted file mode 100644 index e2af5e4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/50fdff1086460011162d85db97d6efcd +++ /dev/null @@ -1,87 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - - size = size - rightTree.size; - - return rightTree; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/707009212f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/707009212f4600111c1cd5b6f02d115c deleted file mode 100644 index b0ed4c5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/707009212f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,5 +0,0 @@ -package test; - -public class Main { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/90aa9917384d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/90aa9917384d0011135ca7e1c9885acd new file mode 100644 index 0000000..5829839 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/90aa9917384d0011135ca7e1c9885acd @@ -0,0 +1,115 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node oldLastNode = tail; + oldLastNode.next = newNode; + } + size++; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/b080545780460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/b080545780460011162d85db97d6efcd deleted file mode 100644 index b913eb2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/b080545780460011162d85db97d6efcd +++ /dev/null @@ -1,26 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - - } - - // METODI - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/d066c5f63a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/d066c5f63a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..6c105df --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/d066c5f63a4d0011135ca7e1c9885acd @@ -0,0 +1,148 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < size; i++) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/f07ef850404600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/e0b0cc478e4c0011135ca7e1c9885acd similarity index 100% rename from .metadata/.plugins/org.eclipse.core.resources/.history/3b/f07ef850404600111c1cd5b6f02d115c rename to .metadata/.plugins/org.eclipse.core.resources/.history/1c/e0b0cc478e4c0011135ca7e1c9885acd diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1d/e05fc4193f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/1d/e05fc4193f4600111c1cd5b6f02d115c deleted file mode 100644 index dd83a09..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1d/e05fc4193f4600111c1cd5b6f02d115c +++ /dev/null @@ -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 - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/4001018a30470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/4001018a30470011162d85db97d6efcd deleted file mode 100644 index 35c1f7f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/4001018a30470011162d85db97d6efcd +++ /dev/null @@ -1,156 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/5076a02b1e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/5076a02b1e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..d9a928f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/5076a02b1e4d0011135ca7e1c9885acd @@ -0,0 +1,12 @@ +package list.mylinkedlist; + +import java.array.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/7091e8325b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/7091e8325b490011162d85db97d6efcd deleted file mode 100644 index 6ffca48..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/7091e8325b490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - //newNode.setParent(currentNode); - - if (wing == 0) { - // Left Wing - newNode.setParentAsLeftChild(currentNode); - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/c0a3f7d33c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/c0a3f7d33c4600111c1cd5b6f02d115c deleted file mode 100644 index d807702..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/c0a3f7d33c4600111c1cd5b6f02d115c +++ /dev/null @@ -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); -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/f0f179f12c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/f0f179f12c470011162d85db97d6efcd deleted file mode 100644 index 813a42f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/f0f179f12c470011162d85db97d6efcd +++ /dev/null @@ -1,103 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1f/200e340cac4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/1f/200e340cac4c0011135ca7e1c9885acd new file mode 100644 index 0000000..c1604e5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1f/200e340cac4c0011135ca7e1c9885acd @@ -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 Map ContaDuplicati(BinaryNode node) { + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + return mappa; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1f/30d306cf7d4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/1f/30d306cf7d4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..8902e00 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1f/30d306cf7d4c00111d9cbe34e2b6027a @@ -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; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2/3034e82969460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2/3034e82969460011162d85db97d6efcd deleted file mode 100644 index ef3eb85..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2/3034e82969460011162d85db97d6efcd +++ /dev/null @@ -1,74 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2/a0b13da340490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2/a0b13da340490011162d85db97d6efcd deleted file mode 100644 index 90b6dcf..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2/a0b13da340490011162d85db97d6efcd +++ /dev/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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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()); - - } - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2/f018554b2b470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2/f018554b2b470011162d85db97d6efcd deleted file mode 100644 index 2c40ab2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2/f018554b2b470011162d85db97d6efcd +++ /dev/null @@ -1,88 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/20/10af506867460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/20/10af506867460011162d85db97d6efcd deleted file mode 100644 index 5b4b7b1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/20/10af506867460011162d85db97d6efcd +++ /dev/null @@ -1,25 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode right) { - this.data = data; - } - - // METODI - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/20/2084099065460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/20/2084099065460011162d85db97d6efcd deleted file mode 100644 index 3c360ac..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/20/2084099065460011162d85db97d6efcd +++ /dev/null @@ -1,7 +0,0 @@ -package binary_tree; - -public interface BinaryTree { - - public boolean isEmpty(); - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/20/a06df8b11b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/20/a06df8b11b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..145d4b8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/20/a06df8b11b4d0011135ca7e1c9885acd @@ -0,0 +1,40 @@ +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 + */ + + public static void main(String[] main) { + + } + + public void run() { + TreeMap mappaAssociazione = new TreeMap(); + + mappaAssociazione.put("Rossi Marco", 8); + mappaAssociazione.put("Bianchi Luca", 14); + mappaAssociazione.put("Esposito Andrea", 6); + mappaAssociazione.put("Ferrari Matteo", 6); + mappaAssociazione.put("Romano Giulia", 14); + mappaAssociazione.put("Ricci Alessia", 6); + + System.out.println("Stampa mappa associazione: "); + System.out.println(mappaAssociazione); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/20/b0bcb82a3f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/20/b0bcb82a3f4600111c1cd5b6f02d115c deleted file mode 100644 index e514122..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/20/b0bcb82a3f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,29 +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 - */ - @Override - public - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/20/d0de926f6a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/20/d0de926f6a460011162d85db97d6efcd deleted file mode 100644 index aa4db0d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/20/d0de926f6a460011162d85db97d6efcd +++ /dev/null @@ -1,82 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (this.parent == null) return null; - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - return oldLeft; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/20/e0aa186b76460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/20/e0aa186b76460011162d85db97d6efcd deleted file mode 100644 index 2a26f2d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/20/e0aa186b76460011162d85db97d6efcd +++ /dev/null @@ -1,138 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - - while (cur.parent != null){ - d++; - cur = cur.parent; - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/21/008bd7082d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/21/008bd7082d470011162d85db97d6efcd deleted file mode 100644 index 5b8eb1b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/21/008bd7082d470011162d85db97d6efcd +++ /dev/null @@ -1,105 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - if (temp != null) { - - } else return false; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/21/00cb084e804c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/21/00cb084e804c00111d9cbe34e2b6027a new file mode 100644 index 0000000..f090b64 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/21/00cb084e804c00111d9cbe34e2b6027a @@ -0,0 +1,67 @@ +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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(archivio); + return archivioOrdinato; + } + + // 6. Stampa su video l'archivio attuale + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/21/70a4ede966460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/21/70a4ede966460011162d85db97d6efcd deleted file mode 100644 index 04a5089..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/21/70a4ede966460011162d85db97d6efcd +++ /dev/null @@ -1,5 +0,0 @@ -package binary_tree; - -public class BinaryNode { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/22/00e13d4a85460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/22/00e13d4a85460011162d85db97d6efcd deleted file mode 100644 index 4a49194..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/22/00e13d4a85460011162d85db97d6efcd +++ /dev/null @@ -1,71 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - - return leftTree; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/22/108825d7034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/22/108825d7034c00111d9cbe34e2b6027a deleted file mode 100644 index 22dff0f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/22/108825d7034c00111d9cbe34e2b6027a +++ /dev/null @@ -1,50 +0,0 @@ -package jcf_set.exercise; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; - -public class IteratoreSenzaDuplicati { - - public static void main(String[] main) { - new IteratoreSenzaDuplicati().run(); - } - - public void run() { - - List lista = new ArrayList(); - - lista.add("tree"); - lista.add("flower"); - lista.add("tree"); - lista.add("flower"); - lista.add("animal"); - lista.add("flower"); - lista.add("fruit"); - - - - } - - // Esercizio 1 - /* - * Passare una lista con duplicati. - * Ottenere un iteratore privo di duplicati. - */ - private static > Iterator getIteratorNoDuplicates(Iterator it) { - HashSet tmp = new HashSet(); - while (it.hasNext()) tmp.add(it.next()); - return tmp.iterator(); - } - - // Esercizio 2 - /* - * Passare come parametro una lista con duplicati. - * Ottenere un iteratore senza duplicati e ordinato. - */ - private static > Iterator getIteratorNoDuplicatesOrdinated(Iterator it) { - TreeSet tmp = new TreeSet(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/22/80005ff7024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/22/80005ff7024c00111d9cbe34e2b6027a deleted file mode 100644 index f57494d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/22/80005ff7024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,30 +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 lista = new ArrayList(); - - lista.add("tree"); - lista.add("flower"); - lista.add("tree"); - lista.add("flower"); - lista.add("animal"); - lista.add("flower"); - lista.add("fruit"); - - - - } - - // Esercizio 1 - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/22/804feb72374d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/22/804feb72374d0011135ca7e1c9885acd new file mode 100644 index 0000000..bfde662 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/22/804feb72374d0011135ca7e1c9885acd @@ -0,0 +1,113 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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(); + if (size == 0) { + + } else { + + } + size++; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/22/e0196522034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/22/e0196522034c00111d9cbe34e2b6027a deleted file mode 100644 index aebde83..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/22/e0196522034c00111d9cbe34e2b6027a +++ /dev/null @@ -1,37 +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 lista = new ArrayList(); - - 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 Iterator getIteratorNoDuplicates() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/23/5027649b314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/23/5027649b314600111c1cd5b6f02d115c deleted file mode 100644 index 9b45523..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/23/5027649b314600111c1cd5b6f02d115c +++ /dev/null @@ -1,26 +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; - } - - /** - * Funzione aggiungi - */ - public boolean aggiungi(Object elemento) { - if (elemento != null && dimensioneCorrente < dimensioneMassima) { - dimensioneCorrente++; - return true; - } else return false; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/23/70ade3e03c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/23/70ade3e03c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..1c26756 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/23/70ade3e03c4d0011135ca7e1c9885acd @@ -0,0 +1,180 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + return currentNode.data; + } + + public E set(int index, E item) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + + Node currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + E oldData = currentNode.data; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/23/b0a5aaef67460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/23/b0a5aaef67460011162d85db97d6efcd deleted file mode 100644 index 4ac1be1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/23/b0a5aaef67460011162d85db97d6efcd +++ /dev/null @@ -1,46 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/23/c0d1ce89404600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/23/c0d1ce89404600111c1cd5b6f02d115c deleted file mode 100644 index 069b3c6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/23/c0d1ce89404600111c1cd5b6f02d115c +++ /dev/null @@ -1,15 +0,0 @@ -package vettore_ordinabile; - -public class Main { - - public static void main (String[] Args) { - - System.out.println("\nTest VettoreIntero"); - Integer i1 = Integer.valueOf(3); - Integer i2 = Integer.valueOf(389); - Integer i3 = Integer.valueOf(15); - Integer i4 = Integer.valueOf(10); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/23/d0d0335c8f4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/23/d0d0335c8f4c0011135ca7e1c9885acd new file mode 100644 index 0000000..ab38437 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/23/d0d0335c8f4c0011135ca7e1c9885acd @@ -0,0 +1,22 @@ +package jcf_map.exercise; + +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/24/40260ba25a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/24/40260ba25a490011162d85db97d6efcd deleted file mode 100644 index d51430c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/24/40260ba25a490011162d85db97d6efcd +++ /dev/null @@ -1,442 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - if (wing == 0) { - // Left Wing - - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/24/70d3c00d2c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/24/70d3c00d2c470011162d85db97d6efcd deleted file mode 100644 index 269c3a3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/24/70d3c00d2c470011162d85db97d6efcd +++ /dev/null @@ -1,93 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals()) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/24/8045b8de374600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/24/8045b8de374600111c1cd5b6f02d115c deleted file mode 100644 index 5084cae..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/24/8045b8de374600111c1cd5b6f02d115c +++ /dev/null @@ -1,40 +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; - } - - /** - * 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; - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/24/90e76d9a3e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/24/90e76d9a3e490011162d85db97d6efcd deleted file mode 100644 index b3de34a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/24/90e76d9a3e490011162d85db97d6efcd +++ /dev/null @@ -1,303 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(current); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/25/20b0acbb3f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/25/20b0acbb3f490011162d85db97d6efcd deleted file mode 100644 index 52d678e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/25/20b0acbb3f490011162d85db97d6efcd +++ /dev/null @@ -1,313 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - - queueOfNodes.add(node); - flags.push(); - - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - // Aggiungiamo il nodo di destra - if (current.getRight() != null) queueOfNodes.push(current.getRight()); - // Aggiungiamo - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/25/3096b49281460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/25/3096b49281460011162d85db97d6efcd deleted file mode 100644 index 1e8d18c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/25/3096b49281460011162d85db97d6efcd +++ /dev/null @@ -1,40 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 false; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/25/7093adeb364d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/25/7093adeb364d0011135ca7e1c9885acd new file mode 100644 index 0000000..77fc35f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/25/7093adeb364d0011135ca7e1c9885acd @@ -0,0 +1,99 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node penultimateNode = tail.prev; + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/25/70a570e785460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/25/70a570e785460011162d85db97d6efcd deleted file mode 100644 index a9ea524..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/25/70a570e785460011162d85db97d6efcd +++ /dev/null @@ -1,81 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/25/8073d745314d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/25/8073d745314d0011135ca7e1c9885acd new file mode 100644 index 0000000..730b57d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/25/8073d745314d0011135ca7e1c9885acd @@ -0,0 +1,59 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/26/00311649034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/26/00311649034c00111d9cbe34e2b6027a deleted file mode 100644 index a1d42fc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/26/00311649034c00111d9cbe34e2b6027a +++ /dev/null @@ -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 lista = new ArrayList(); - - 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 Iterator getIteratorNoDuplicates(Iterator it) { - HashSet tmp = - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/26/40665d03aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/26/40665d03aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..d97c883 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/26/40665d03aa4c0011135ca7e1c9885acd @@ -0,0 +1,22 @@ +package jcf_map.exercise; + +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/26/d0823244414d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/26/d0823244414d0011135ca7e1c9885acd new file mode 100644 index 0000000..628b392 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/26/d0823244414d0011135ca7e1c9885acd @@ -0,0 +1,170 @@ +package parziale.p251110; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.TreeMap; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + ListIterator lit = pazienti.listIterator(); + while (lit.hasNext()) { + Paziente current = lit.next(); + int cmp = current.compareTo(paziente); + if (cmp > 0) { + lit.previous(); + lit.add(paziente); + return true; + } + } + + lit.add(paziente); + return true; + } + + // 3. Dimissione + public Paziente dimettiPaziente(String id, int annoNascita) { + if (id == null || annoNascita <= 0) return null; + Paziente tmp = new Paziente(id, annoNascita); + + Iterator iterator = pazienti.iterator(); + while (iterator.hasNext()) { + Paziente current = iterator.next(); + int cmp = current.compareTo(tmp); + if (cmp == 0) { + // Paziente da rimuovere + iterator.remove(); + return current; + } + if (cmp > 0) return null; + } + + return null; + } + + // 4. Età media + public int etaMedia() { + if (pazienti.isEmpty()) return 0; + + int currentDate = LocalDate.now().getYear(); + int avg = 0; + + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + int ageYear = currentDate - it.next().getAnnoNascita(); + avg += ageYear; + } + + return avg / pazienti.size(); + } + + // 5. Età più rappresenta + public int etaMediaPiuRappresentata() { + if (pazienti.isEmpty()) return 0; + + int currentDate = LocalDate.now().getYear(); + TreeMap listaEta = new TreeMap(); + + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + int currentAge = currentDate - current.getAnnoNascita(); + + if (listaEta.get(currentAge) == null) listaEta.put(currentAge, 1); + else listaEta.put(currentAge, listaEta.get(currentAge) + 1); + } + + int maxAge = 0; + int maxFrequency = 0; + + for (Integer i : listaEta.keySet()) { + int currentFrequency = listaEta.get(i); + + if (currentFrequency > maxFrequency) { + maxFrequency = currentFrequency; + maxAge = i; + } + + } + + return maxAge; + } + + // 6. Ricoverati ordinati per codice + public ArrayList ricoveratiOrdinatiPerCodice() { + ArrayList ricoverati = new ArrayList(pazienti); + + ricoverati.sort(new Comparator() { + @Override + public int compare(Paziente p1, Paziente p2) { + String id1 = p1.getId(); + String id2 = p2.getId(); + return id1.compareTo(id2); + } + }); + + return ricoverati; + } + + // 7. Distribuzione pazienti per anno di nascita + public Map pazientiPerAnnoDiNascita() { + // AnnoNascita, NumeroPazienti + Map pazientiPerAnnoDiNascita = new TreeMap(); // Si usa TreeMap per avere una vista già ordinata di anni + + if (pazienti.isEmpty()) return pazientiPerAnnoDiNascita; + + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + int annoNascita = current.getAnnoNascita(); + + if (pazientiPerAnnoDiNascita.get(annoNascita) == null) pazientiPerAnnoDiNascita.put(annoNascita, 1); + else pazientiPerAnnoDiNascita.put(annoNascita, pazientiPerAnnoDiNascita.get(annoNascita) + 1); + } + + return pazientiPerAnnoDiNascita; + } + + public static List ordinaListaClinicaPerNumeroPazienti(List cliniche) { + if (cliniche == null) throw new NullPointerException(); + + cliniche.sort(new Comparator() { + @Override + public int compare(Clinica c1, Clinica c2) { + int pazienti1 = c1.pazienti.size(); + int pazienti2 = c2.pazienti.size(); + return pazienti1 - pazienti2; + } + }); + + return cliniche; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/27/100aff2681460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/27/100aff2681460011162d85db97d6efcd deleted file mode 100644 index 78edc41..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/27/100aff2681460011162d85db97d6efcd +++ /dev/null @@ -1,40 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(left.root, data, right.root); - size = 1 + left.size + right.size; - } - - // METODI - - public int size() { - return size; - } - - public boolean isEmpty() { - return size == 0; - } - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/27/20c4fff3ab4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/27/20c4fff3ab4c0011135ca7e1c9885acd new file mode 100644 index 0000000..201d3dc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/27/20c4fff3ab4c0011135ca7e1c9885acd @@ -0,0 +1,39 @@ +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 Map ContaDuplicati(BinaryNode node) { + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + return mappa; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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(data, 1); + else mappa.put(data, mappa.get(data) + 1); + + return mappa; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/27/60ac05aa354d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/27/60ac05aa354d0011135ca7e1c9885acd new file mode 100644 index 0000000..a3adcbe --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/27/60ac05aa354d0011135ca7e1c9885acd @@ -0,0 +1,88 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/27/8065df187e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/27/8065df187e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..2b6e01f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/27/8065df187e4c00111d9cbe34e2b6027a @@ -0,0 +1,37 @@ +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) { + 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() { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/27/90ee430086460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/27/90ee430086460011162d85db97d6efcd deleted file mode 100644 index bbdba77..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/27/90ee430086460011162d85db97d6efcd +++ /dev/null @@ -1,86 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - - size = size - rightTree.size; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/27/c0046786374600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/27/c0046786374600111c1cd5b6f02d115c deleted file mode 100644 index 2462b4a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/27/c0046786374600111c1cd5b6f02d115c +++ /dev/null @@ -1,36 +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; - } - - /** - * 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 () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/27/d0b5b782314d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/27/d0b5b782314d0011135ca7e1c9885acd new file mode 100644 index 0000000..c9da833 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/27/d0b5b782314d0011135ca7e1c9885acd @@ -0,0 +1,60 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/27/f0494b33314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/27/f0494b33314600111c1cd5b6f02d115c deleted file mode 100644 index 8c9e943..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/27/f0494b33314600111c1cd5b6f02d115c +++ /dev/null @@ -1,16 +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; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/28/207469946b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/28/207469946b460011162d85db97d6efcd deleted file mode 100644 index f3a5ae4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/28/207469946b460011162d85db97d6efcd +++ /dev/null @@ -1,114 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/28/702c55b85a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/28/702c55b85a490011162d85db97d6efcd deleted file mode 100644 index c6b4ab4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/28/702c55b85a490011162d85db97d6efcd +++ /dev/null @@ -1,444 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode; - - if (wing == 0) { - // Left Wing - - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/00546cd57f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/29/00546cd57f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..b2c67fd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/29/00546cd57f4c00111d9cbe34e2b6027a @@ -0,0 +1,68 @@ +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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(new Comparator() { + @Override + public int compare() { + + } + }); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/2027f94b5a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/29/2027f94b5a490011162d85db97d6efcd deleted file mode 100644 index 5ddce91..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/29/2027f94b5a490011162d85db97d6efcd +++ /dev/null @@ -1,443 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject - - while (iterator.hasNext()) { - E currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - currentNode.setData(currentObject); - - if (wing == 0) { - // Left Wing - - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/2051e95e2f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/29/2051e95e2f470011162d85db97d6efcd deleted file mode 100644 index 6a9d00d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/29/2051e95e2f470011162d85db97d6efcd +++ /dev/null @@ -1,129 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/2054227e6b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/29/2054227e6b460011162d85db97d6efcd deleted file mode 100644 index 17cea95..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/29/2054227e6b460011162d85db97d6efcd +++ /dev/null @@ -1,108 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode oldParent = parent; - parent = null; - - if (oldParent.left == this) oldParent.left = null; - else oldParent.right = null; - - return oldParent; - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/306179641e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/29/306179641e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..8551566 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/29/306179641e4d0011135ca7e1c9885acd @@ -0,0 +1,20 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Data + private E data; + + // Riferimenti elemento precedente e successivo + private Node + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/502c7cc91b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/29/502c7cc91b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..c85cefc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/29/502c7cc91b4d0011135ca7e1c9885acd @@ -0,0 +1,42 @@ +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 + */ + + public static void main(String[] main) { + new CompanyMap().run(); + } + + public void run() { + TreeMap mappaAssociazione = new TreeMap(); + + mappaAssociazione.put("Rossi Marco", 8); + mappaAssociazione.put("Bianchi Luca", 14); + mappaAssociazione.put("Esposito Andrea", 6); + mappaAssociazione.put("Ferrari Matteo", 6); + mappaAssociazione.put("Romano Giulia", 14); + mappaAssociazione.put("Ricci Alessia", 6); + + System.out.println("Stampa mappa associazione: "); + System.out.println(mappaAssociazione); + System.out.println("Numero di dipendenti: " + mappaAssociazione.size()); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/503997125e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/29/503997125e490011162d85db97d6efcd deleted file mode 100644 index db80a44..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/29/503997125e490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 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 node) { - if (node == null) return 0; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/74/600a9e613e4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/29/50b0c3811a4d0011135ca7e1c9885acd similarity index 100% rename from .metadata/.plugins/org.eclipse.core.resources/.history/74/600a9e613e4600111c1cd5b6f02d115c rename to .metadata/.plugins/org.eclipse.core.resources/.history/29/50b0c3811a4d0011135ca7e1c9885acd diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/50d091b5314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/29/50d091b5314600111c1cd5b6f02d115c deleted file mode 100644 index 4aacb98..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/29/50d091b5314600111c1cd5b6f02d115c +++ /dev/null @@ -1,27 +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; - } - - /** - * Funzione aggiungi - */ - public boolean aggiungi(Object elemento) { - if (elemento != null && dimensioneCorrente < dimensioneMassima) { - vettore[dimensioneCorrente] = elemento; - dimensioneCorrente++; - return true; - } else return false; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/60236c0a804c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/29/60236c0a804c00111d9cbe34e2b6027a new file mode 100644 index 0000000..3dd7f07 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/29/60236c0a804c00111d9cbe34e2b6027a @@ -0,0 +1,68 @@ +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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(new Comparator() { + @Override + public int compare(String s1, String s2) { + return s1.compareTo(s2); + } + }); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/80a523b6304600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/29/80a523b6304600111c1cd5b6f02d115c deleted file mode 100644 index 783a71d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/29/80a523b6304600111c1cd5b6f02d115c +++ /dev/null @@ -1,9 +0,0 @@ -package vettore_ordinabile; - -public abstract class VettoreOrdinabile { - - // Variabili di istanza - private Object[] vettore; // Vettore dove memorizzare i dati - private int dimensioneMassima; - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/900e07dc3f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/29/900e07dc3f4600111c1cd5b6f02d115c deleted file mode 100644 index 033bb76..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/29/900e07dc3f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,51 +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 - */ - @Override - public boolean aggiungi(Object object) { - return false; - } - - // Metodo aggiungi - /* - * Questo metodo aggiunge realmente l'elemento di tipo - * Integer - */ - public boolean aggiungi(Integer integer) { - return super.aggiungi(integer); - } - - // Metodo ordina - /* - * Questo metodo permette il funzionamento dell'ordinamento - * tramite comparatore simulato - */ - @Override - public int ordina(Object elemento1, Object elemento2) { - Integer i1 = (Integer)elemento1; - Integer i2 = (Integer)elemento2; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/d06f675c404600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/29/d06f675c404600111c1cd5b6f02d115c deleted file mode 100644 index 6225d1b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/29/d06f675c404600111c1cd5b6f02d115c +++ /dev/null @@ -1,5 +0,0 @@ -package vettore_ordinabile; - -public class Main { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/d0d2aa9b69460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/29/d0d2aa9b69460011162d85db97d6efcd deleted file mode 100644 index 7465496..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/29/d0d2aa9b69460011162d85db97d6efcd +++ /dev/null @@ -1,74 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2a/9016855b76460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/9016855b76460011162d85db97d6efcd deleted file mode 100644 index d174dcb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2a/9016855b76460011162d85db97d6efcd +++ /dev/null @@ -1,133 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2a/f0ee062d804c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/f0ee062d804c00111d9cbe34e2b6027a new file mode 100644 index 0000000..cf65040 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/f0ee062d804c00111d9cbe34e2b6027a @@ -0,0 +1,65 @@ +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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(archivio); + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2b/009d006b2f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/009d006b2f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..0ec4c01 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/009d006b2f4d0011135ca7e1c9885acd @@ -0,0 +1,48 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node tail = null; + private int size = 0; + + // Costruttore + public MyLinkedList() {} + + // Metodi + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } + + public E getFirst() { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2b/10895d60aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/10895d60aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..4522b2a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/10895d60aa4c0011135ca7e1c9885acd @@ -0,0 +1,28 @@ +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + if (node == null) return mappa; + + if (node.getLeft() != null) ContaDuplicati(node.getLeft(), mappa); + if (node.getRight() != null) ContaDuplicati(node.getRight(), mappa); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2b/10e2d00a32490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/10e2d00a32490011162d85db97d6efcd deleted file mode 100644 index c005f2f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2b/10e2d00a32490011162d85db97d6efcd +++ /dev/null @@ -1,241 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator iterator() { - return iteratorPreOrder(); - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2b/9060a15baa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/9060a15baa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..4160e53 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/9060a15baa4c0011135ca7e1c9885acd @@ -0,0 +1,28 @@ +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + if (node == null) return mappa; + + if (node.getLeft() != null) ContaDuplicati(node.getLeft(), mappa); + if (node.getRight() != null) ContaDuplicati(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2b/a04f1c255b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/a04f1c255b490011162d85db97d6efcd deleted file mode 100644 index cf6a147..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2b/a04f1c255b490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - //newNode.setParent(currentNode); - - if (wing == 0) { - // Left Wing - currentNode. - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2b/b07d741c7f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/b07d741c7f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..ef8fcdc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/b07d741c7f4c00111d9cbe34e2b6027a @@ -0,0 +1,52 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + for (String s : archivio.keySet()) { + Cliente currentCliente = archivio.get(s); + String currentCitta = currentCliente.citta(); + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2c/30879c0c9c4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/30879c0c9c4c0011135ca7e1c9885acd new file mode 100644 index 0000000..24eed61 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/30879c0c9c4c0011135ca7e1c9885acd @@ -0,0 +1,37 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + if (myList.isEmpty()) return true; + // Verifica che la lista si pari o dispari + HashMap valori = new HashMap(); + + Iterator 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); + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2c/7064b5ea6a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/7064b5ea6a460011162d85db97d6efcd deleted file mode 100644 index 97490a1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2c/7064b5ea6a460011162d85db97d6efcd +++ /dev/null @@ -1,98 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2c/908c974b334d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/908c974b334d0011135ca7e1c9885acd new file mode 100644 index 0000000..56a77e7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/908c974b334d0011135ca7e1c9885acd @@ -0,0 +1,76 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 { + head.next.prev = null; + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2c/d0048e5b984c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/d0048e5b984c0011135ca7e1c9885acd new file mode 100644 index 0000000..15ee7c1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/d0048e5b984c0011135ca7e1c9885acd @@ -0,0 +1,26 @@ +package jcf_map.exercise; + +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2d/50c9b6775d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/50c9b6775d490011162d85db97d6efcd deleted file mode 100644 index 00ae017..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2d/50c9b6775d490011162d85db97d6efcd +++ /dev/null @@ -1,484 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public void numberLeaf() { - Integer number = 0; - numberLeaf(root, number); - } - - public void numberLeaf(BinaryNode node, Integer number) { - if (node == null) return; - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2d/80cae89c3c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/80cae89c3c4600111c1cd5b6f02d115c deleted file mode 100644 index 2208831..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2d/80cae89c3c4600111c1cd5b6f02d115c +++ /dev/null @@ -1,77 +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; - for (int j = i+1; j < dimensioneCorrente; j++) { - if (confronta(vettore[minimo], vettore[j]) > 0) minimo = j; - - } - - } - } - - // FUNZIONI ASTRATTE - - /** - * Funzione che restituisce il valore della comparazione - */ - protected abstract int confronta(Object elemento1, Object elemento2); -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2d/a00f7051354d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/a00f7051354d0011135ca7e1c9885acd new file mode 100644 index 0000000..2f01896 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/a00f7051354d0011135ca7e1c9885acd @@ -0,0 +1,83 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node secondPlace = head.next; + + head = secondPlace; + secondPlace.prev = null; + firstPlace.next = null; + } + size--; + return tmp; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2d/b01fc5e05c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/b01fc5e05c490011162d85db97d6efcd deleted file mode 100644 index 5c455c7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2d/b01fc5e05c490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - - } - - public void printLeaf(BinaryNode node) { - if (node == null) return; - - if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); - else { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2d/c00ff1f54e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/c00ff1f54e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..c1e8f08 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/c00ff1f54e4d0011135ca7e1c9885acd @@ -0,0 +1,183 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + return currentNode.data; + } + + public E set(int index, E item) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + + Node currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + E oldData = currentNode.data; + currentNode.data = item; + return oldData; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2e/00ed20543e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/00ed20543e490011162d85db97d6efcd deleted file mode 100644 index 86c3e12..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2e/00ed20543e490011162d85db97d6efcd +++ /dev/null @@ -1,299 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2e/600dc8273a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/600dc8273a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..8d5eb6d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/600dc8273a4d0011135ca7e1c9885acd @@ -0,0 +1,143 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2e/604ff500384d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/604ff500384d0011135ca7e1c9885acd new file mode 100644 index 0000000..fd6552f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/604ff500384d0011135ca7e1c9885acd @@ -0,0 +1,114 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node oldLastNode = tail; + } + size++; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2e/d04448ea3b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/d04448ea3b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..f8c9ba3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/d04448ea3b4d0011135ca7e1c9885acd @@ -0,0 +1,159 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node nextNode = prevNode.next; + + prevNode.next = newNode; + newNode.prev = prevNode; + newNode.next = nextNode; + nextNode.prev = newNode; + + size++; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2e/f01de0c92b470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/f01de0c92b470011162d85db97d6efcd deleted file mode 100644 index bd747d2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2e/f01de0c92b470011162d85db97d6efcd +++ /dev/null @@ -1,90 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2e/f0c04f6e5e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/f0c04f6e5e490011162d85db97d6efcd deleted file mode 100644 index 35bd981..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2e/f0c04f6e5e490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 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 node) { - if (node == null) return 0; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2f/3033617531470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2f/3033617531470011162d85db97d6efcd deleted file mode 100644 index 032ceee..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2f/3033617531470011162d85db97d6efcd +++ /dev/null @@ -1,172 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - inorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE POSTORDER - - /* - * Qui si utilizza il metodo postorder. - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2f/d0d9fcef66460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2f/d0d9fcef66460011162d85db97d6efcd deleted file mode 100644 index 656eed2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2f/d0d9fcef66460011162d85db97d6efcd +++ /dev/null @@ -1,9 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2f/e0e8b50c1b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/2f/e0e8b50c1b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..548ba7a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2f/e0e8b50c1b4d0011135ca7e1c9885acd @@ -0,0 +1,18 @@ +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: si intende adoperare chiaramente un TreeMap. + * In modo che vi sia una associazione chiave-valore e un ordinamento basato + * sulla chiave. + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3/10b05e8535470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3/10b05e8535470011162d85db97d6efcd deleted file mode 100644 index 15fcc12..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3/10b05e8535470011162d85db97d6efcd +++ /dev/null @@ -1,225 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode current; - - queueOfNodes.add(node); - while(!queueOfNodes.isEmpty()) { - current = queueOfNodes.remove(); - - if (current.getData() != null) temporaryList.add(current.getData()); - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3/50f7940050490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3/50f7940050490011162d85db97d6efcd deleted file mode 100644 index 10153c9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3/50f7940050490011162d85db97d6efcd +++ /dev/null @@ -1,409 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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. - */ - public LinkedBinaryTree () { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3/a0dd3419304d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/3/a0dd3419304d0011135ca7e1c9885acd new file mode 100644 index 0000000..eb1f9b3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3/a0dd3419304d0011135ca7e1c9885acd @@ -0,0 +1,59 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 + } else { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3/d00b22df324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/3/d00b22df324d0011135ca7e1c9885acd new file mode 100644 index 0000000..9802d36 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3/d00b22df324d0011135ca7e1c9885acd @@ -0,0 +1,77 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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) { + tmp = head.data; + head = tail = null; + } else { + + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3/d00ce60a76460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3/d00ce60a76460011162d85db97d6efcd deleted file mode 100644 index aa8d714..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3/d00ce60a76460011162d85db97d6efcd +++ /dev/null @@ -1,126 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/1029016641490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/30/1029016641490011162d85db97d6efcd deleted file mode 100644 index ef3256a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/30/1029016641490011162d85db97d6efcd +++ /dev/null @@ -1,341 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - itinorder(root, temporaryList); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/30608b86384600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/30/30608b86384600111c1cd5b6f02d115c deleted file mode 100644 index 750f92a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/30/30608b86384600111c1cd5b6f02d115c +++ /dev/null @@ -1,56 +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 ordina l'array - */ - public void ordina() { - - } - - // - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/508693975d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/30/508693975d490011162d85db97d6efcd deleted file mode 100644 index 70b4aa6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/30/508693975d490011162d85db97d6efcd +++ /dev/null @@ -1,487 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public void numberLeaf() { - Integer number = 0; - numberLeaf(root, number); - } - - public void numberLeaf(BinaryNode node, Integer number) { - if (node == null) return; - - if (node.getLeft() == null && node.getRight() == null) number++; - else { - numberLeaf(node.getLeft(), number); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/a05c728081460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/30/a05c728081460011162d85db97d6efcd deleted file mode 100644 index d13fe36..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/30/a05c728081460011162d85db97d6efcd +++ /dev/null @@ -1,38 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(left.root, data, right.root); - size = 1 + left.size + right.size; - } - - // METODI - - public int size() { - return size; - } - - @Override - public boolean isEmpty() { - return root == null; - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/c0e900e45c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/30/c0e900e45c490011162d85db97d6efcd deleted file mode 100644 index 31e0fd8..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/30/c0e900e45c490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - - } - - public void printLeaf(BinaryNode node) { - if (node == null) return; - - if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); - else { - printLeaf(); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/f024b70943490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/30/f024b70943490011162d85db97d6efcd deleted file mode 100644 index 560dac0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/30/f024b70943490011162d85db97d6efcd +++ /dev/null @@ -1,375 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/f081edd52f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/30/f081edd52f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..630179b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/30/f081edd52f4d0011135ca7e1c9885acd @@ -0,0 +1,54 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/31/101e633c76460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/31/101e633c76460011162d85db97d6efcd deleted file mode 100644 index d857c4b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/31/101e633c76460011162d85db97d6efcd +++ /dev/null @@ -1,128 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/31/5021a0f7384d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/31/5021a0f7384d0011135ca7e1c9885acd new file mode 100644 index 0000000..70987af --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/31/5021a0f7384d0011135ca7e1c9885acd @@ -0,0 +1,126 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/31/801ddaf83e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/31/801ddaf83e490011162d85db97d6efcd deleted file mode 100644 index 04fce2a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/31/801ddaf83e490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - if (current.getRight() != null) - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/31/80962c2f3d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/31/80962c2f3d490011162d85db97d6efcd deleted file mode 100644 index d275ddc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/31/80962c2f3d490011162d85db97d6efcd +++ /dev/null @@ -1,274 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 il primo 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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()); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/31/9059d68d5c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/31/9059d68d5c490011162d85db97d6efcd deleted file mode 100644 index fa17a9d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/31/9059d68d5c490011162d85db97d6efcd +++ /dev/null @@ -1,463 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - - } - - public void printLeaf(BinaryNode node) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/31/e07e11da5a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/31/e07e11da5a490011162d85db97d6efcd deleted file mode 100644 index b030917..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/31/e07e11da5a490011162d85db97d6efcd +++ /dev/null @@ -1,444 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left Wing - - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/31/e0852927aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/31/e0852927aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..1150f55 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/31/e0852927aa4c0011135ca7e1c9885acd @@ -0,0 +1,24 @@ +package jcf_map.exercise; + +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap ) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/31/e09a142e2f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/31/e09a142e2f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..a835c46 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/31/e09a142e2f4d0011135ca7e1c9885acd @@ -0,0 +1,37 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node tail = null; + private int size = 0; + + // Costruttore + public MuLinkedList() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/32/20214def40490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/32/20214def40490011162d85db97d6efcd deleted file mode 100644 index 5275ec0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/32/20214def40490011162d85db97d6efcd +++ /dev/null @@ -1,328 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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); - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/32/30c53a3e2f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/32/30c53a3e2f470011162d85db97d6efcd deleted file mode 100644 index d8a478a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/32/30c53a3e2f470011162d85db97d6efcd +++ /dev/null @@ -1,127 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/33/20555aab3e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/33/20555aab3e490011162d85db97d6efcd deleted file mode 100644 index 2c98457..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/33/20555aab3e490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/33/50183093404600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/33/50183093404600111c1cd5b6f02d115c deleted file mode 100644 index a10bf92..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/33/50183093404600111c1cd5b6f02d115c +++ /dev/null @@ -1,17 +0,0 @@ -package vettore_ordinabile; - -public class Main { - - public static void main (String[] Args) { - - System.out.println("\nTest VettoreIntero"); - Integer i1 = Integer.valueOf(3); - Integer i2 = Integer.valueOf(389); - Integer i3 = Integer.valueOf(15); - Integer i4 = Integer.valueOf(10); - - VettoriIntero VI = new VettoriIntero(4); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/34/305e48dc9e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/34/305e48dc9e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..25881e1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/34/305e48dc9e4c0011135ca7e1c9885acd @@ -0,0 +1,51 @@ +package jcf_map.exercise; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari> { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/34/5030ac8776460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/34/5030ac8776460011162d85db97d6efcd deleted file mode 100644 index 8c1828b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/34/5030ac8776460011162d85db97d6efcd +++ /dev/null @@ -1,140 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - - while (cur.parent != null){ - d++; - cur = cur.parent; - } - - return d; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/34/609f716535470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/34/609f716535470011162d85db97d6efcd deleted file mode 100644 index 9280718..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/34/609f716535470011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode current; - - queueOfNodes.add(node); - while(!queueOfNodes.isEmpty()) { - current = queueOfNodes.remove(); - - if (current.getData() != null) - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/34/b0b6c0af34470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/34/b0b6c0af34470011162d85db97d6efcd deleted file mode 100644 index 70d0134..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/34/b0b6c0af34470011162d85db97d6efcd +++ /dev/null @@ -1,214 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/34/f09218291e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/34/f09218291e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..3656c5b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/34/f09218291e4d0011135ca7e1c9885acd @@ -0,0 +1,12 @@ +package list.mylinkedlist; + +implements java.array.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/35/600e04139f4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/35/600e04139f4c0011135ca7e1c9885acd new file mode 100644 index 0000000..abe4ec4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/35/600e04139f4c0011135ca7e1c9885acd @@ -0,0 +1,5 @@ +package jcf_map.exercise; + +public class BinaryTreeMap { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/35/90392959374d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/35/90392959374d0011135ca7e1c9885acd new file mode 100644 index 0000000..56f0f0a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/35/90392959374d0011135ca7e1c9885acd @@ -0,0 +1,107 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/35/904c74e067460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/35/904c74e067460011162d85db97d6efcd deleted file mode 100644 index 16b41a9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/35/904c74e067460011162d85db97d6efcd +++ /dev/null @@ -1,44 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/35/a077af3c5b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/35/a077af3c5b490011162d85db97d6efcd deleted file mode 100644 index 75381d3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/35/a077af3c5b490011162d85db97d6efcd +++ /dev/null @@ -1,446 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - //newNode.setParent(currentNode); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/35/e072a36537470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/35/e072a36537470011162d85db97d6efcd deleted file mode 100644 index 9199865..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/35/e072a36537470011162d85db97d6efcd +++ /dev/null @@ -1,232 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(node, temporaryList); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/35/f0d504391b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/35/f0d504391b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..19cc297 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/35/f0d504391b4d0011135ca7e1c9885acd @@ -0,0 +1,23 @@ +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. + * + */ + + TreeMap + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/36/008629692f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/36/008629692f470011162d85db97d6efcd deleted file mode 100644 index 3bdb680..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/36/008629692f470011162d85db97d6efcd +++ /dev/null @@ -1,129 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/36/607853ba34470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/36/607853ba34470011162d85db97d6efcd deleted file mode 100644 index c614f58..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/36/607853ba34470011162d85db97d6efcd +++ /dev/null @@ -1,219 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/36/8026338343490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/36/8026338343490011162d85db97d6efcd deleted file mode 100644 index b308136..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/36/8026338343490011162d85db97d6efcd +++ /dev/null @@ -1,394 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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()); - flags.push(false); - } - - // visitiamo il nodo di sinistra infine - if (current.getLeft() != null) { - queueOfNodes.push(current.getLeft()); - flags.push(false); - } - } - } - } - - public Iterator ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/36/908d1035a84c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/36/908d1035a84c0011135ca7e1c9885acd new file mode 100644 index 0000000..be5bea9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/36/908d1035a84c0011135ca7e1c9885acd @@ -0,0 +1,5 @@ +package jcf_map.exercise; + +public class BinaryTreeMap { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/36/a05800f573460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/36/a05800f573460011162d85db97d6efcd deleted file mode 100644 index d670fcb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/36/a05800f573460011162d85db97d6efcd +++ /dev/null @@ -1,122 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/36/f0a3caf6ab4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/36/f0a3caf6ab4c0011135ca7e1c9885acd new file mode 100644 index 0000000..4d5c5ad --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/36/f0a3caf6ab4c0011135ca7e1c9885acd @@ -0,0 +1,39 @@ +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 Map ContaDuplicati(BinaryNode node) { + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + return mappa; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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 mappa; + + T data = node.getData(); + if (mappa.get(data) == null) mappa.put(data, 1); + else mappa.put(data, mappa.get(data) + 1); + + return mappa; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/37/201620542f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/37/201620542f470011162d85db97d6efcd deleted file mode 100644 index 37833fa..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/37/201620542f470011162d85db97d6efcd +++ /dev/null @@ -1,129 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - temporaryList.add(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/37/807a73599e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/37/807a73599e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..6fe0946 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/37/807a73599e4c0011135ca7e1c9885acd @@ -0,0 +1,45 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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); + } + for (E chiave : valori.keySet()) { + int valore = valori.get(chiave); + if (valore % 2 == 0) + } + // Controllo short circuit di disparità + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/38/20e7f42068460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/38/20e7f42068460011162d85db97d6efcd deleted file mode 100644 index 6f196b8..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/38/20e7f42068460011162d85db97d6efcd +++ /dev/null @@ -1,52 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode getParent() { - return parent; - } - - public boolean isRoot() { - return parent == null; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/38/40cae98f2d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/38/40cae98f2d470011162d85db97d6efcd deleted file mode 100644 index ea26961..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/38/40cae98f2d470011162d85db97d6efcd +++ /dev/null @@ -1,111 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - if (temp != null) { - temp.setData(null); - return true; - } - return false; - } - - public boolean contains(E targetElement) { - return (); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/38/505233b7374d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/38/505233b7374d0011135ca7e1c9885acd new file mode 100644 index 0000000..b82113c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/38/505233b7374d0011135ca7e1c9885acd @@ -0,0 +1,114 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = new Node(item, null, null); + } else { + Node newNode = new Node(); + } + size++; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/38/6015615c1e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/38/6015615c1e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..8551566 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/38/6015615c1e4d0011135ca7e1c9885acd @@ -0,0 +1,20 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Data + private E data; + + // Riferimenti elemento precedente e successivo + private Node + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/38/707b9bcd2f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/38/707b9bcd2f470011162d85db97d6efcd deleted file mode 100644 index 626c413..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/38/707b9bcd2f470011162d85db97d6efcd +++ /dev/null @@ -1,136 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/38/c004c4de1b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/38/c004c4de1b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..fcde5b5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/38/c004c4de1b4d0011135ca7e1c9885acd @@ -0,0 +1,44 @@ +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 + */ + + public static void main(String[] main) { + new CompanyMap().run(); + } + + public void run() { + TreeMap mappaAssociazione = new TreeMap(); + + mappaAssociazione.put("Rossi Marco", 8); + mappaAssociazione.put("Bianchi Luca", 14); + mappaAssociazione.put("Esposito Andrea", 6); + mappaAssociazione.put("Ferrari Matteo", 6); + mappaAssociazione.put("Romano Giulia", 14); + mappaAssociazione.put("Ricci Alessia", 6); + + System.out.println("Stampa mappa associazione: "); + System.out.println(mappaAssociazione); + System.out.println("Numero di dipendenti: " + mappaAssociazione.size()); + + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/38/d092a59f374600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/38/d092a59f374600111c1cd5b6f02d115c deleted file mode 100644 index c8864f5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/38/d092a59f374600111c1cd5b6f02d115c +++ /dev/null @@ -1,38 +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; - } - - /** - * 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) { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/39/50ddc46130470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/39/50ddc46130470011162d85db97d6efcd deleted file mode 100644 index 3afa073..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/39/50ddc46130470011162d85db97d6efcd +++ /dev/null @@ -1,142 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - preorder(root, temporaryList); - return temporaryList.iterator(); - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/39/6028b60036470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/39/6028b60036470011162d85db97d6efcd deleted file mode 100644 index 7901f6f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/39/6028b60036470011162d85db97d6efcd +++ /dev/null @@ -1,229 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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()); - } - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/39/6037c85e5a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/39/6037c85e5a490011162d85db97d6efcd deleted file mode 100644 index e244808..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/39/6037c85e5a490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - - - - while (iterator.hasNext()) { - E currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - currentNode.setData(currentObject); - - if (wing == 0) { - // Left Wing - - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/39/70135f1169460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/39/70135f1169460011162d85db97d6efcd deleted file mode 100644 index 34238dd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/39/70135f1169460011162d85db97d6efcd +++ /dev/null @@ -1,69 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/39/a0b25a429e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/39/a0b25a429e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..0e35cd0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/39/a0b25a429e4c0011135ca7e1c9885acd @@ -0,0 +1,42 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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); + } + for (E chiave : valori.keySet()) + // Controllo short circuit di disparità + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/39/e010a08e3b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/39/e010a08e3b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..b1e9cc7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/39/e010a08e3b4d0011135ca7e1c9885acd @@ -0,0 +1,155 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node nextNode = prevNode.next; + + prevNode.next = newNode; + newNode.prev = prevNode; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/005ff1f72c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/005ff1f72c470011162d85db97d6efcd deleted file mode 100644 index 7e80b87..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/005ff1f72c470011162d85db97d6efcd +++ /dev/null @@ -1,103 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/00936cc876460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/00936cc876460011162d85db97d6efcd deleted file mode 100644 index 5876ae7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/00936cc876460011162d85db97d6efcd +++ /dev/null @@ -1,146 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - - while (cur.parent != null){ - d++; - cur = cur.parent; - } - - return d; - } - - public int height() { - if (left == null && right == null) return 0; - - int hLeft = - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/709a0c6a68460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/709a0c6a68460011162d85db97d6efcd deleted file mode 100644 index 4f3ff55..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/709a0c6a68460011162d85db97d6efcd +++ /dev/null @@ -1,57 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode getParent() { - return parent; - } - - public boolean isRoot() { - return parent == null; - } - - public boolean isLeftChild() { - if (parent != null && parent.getLeft() == this) return true; - return false; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/00529eea33490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/00529eea33490011162d85db97d6efcd deleted file mode 100644 index 299025a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/00529eea33490011162d85db97d6efcd +++ /dev/null @@ -1,271 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 il primo 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - if (current.getData() != null) temporaryList.add(current.getData()); - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/108f5b067e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/108f5b067e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..fee04d1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/108f5b067e4c00111d9cbe34e2b6027a @@ -0,0 +1,35 @@ +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) { + 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; + } + + // Equals + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/60eeacd59e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/60eeacd59e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..d61b72d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/60eeacd59e4c0011135ca7e1c9885acd @@ -0,0 +1,49 @@ +package jcf_map.exercise; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/7080cb0d5e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/7080cb0d5e490011162d85db97d6efcd deleted file mode 100644 index f6878cc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/7080cb0d5e490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 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 node) { - if (node == null) return - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/b08174893e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/b08174893e490011162d85db97d6efcd deleted file mode 100644 index 2086e8b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/b08174893e490011162d85db97d6efcd +++ /dev/null @@ -1,300 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/d023eb9c344d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/d023eb9c344d0011135ca7e1c9885acd new file mode 100644 index 0000000..2102fe4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/d023eb9c344d0011135ca7e1c9885acd @@ -0,0 +1,78 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 { + head.next.prev = null; + head = head.next; + + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3c/10dd6882324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/3c/10dd6882324d0011135ca7e1c9885acd new file mode 100644 index 0000000..d57eb7d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3c/10dd6882324d0011135ca7e1c9885acd @@ -0,0 +1,68 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + newNode.next = head; + head = newNode; + } + size++; + } + + public E removeFirst() { + if (head == null) throw new NoSuchElementException(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3c/309060ab2f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/3c/309060ab2f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..ae4af8a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3c/309060ab2f4d0011135ca7e1c9885acd @@ -0,0 +1,52 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3c/709479b22f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/3c/709479b22f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..1864f09 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3c/709479b22f4d0011135ca7e1c9885acd @@ -0,0 +1,54 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3c/805f50da324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/3c/805f50da324d0011135ca7e1c9885acd new file mode 100644 index 0000000..8a3c574 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3c/805f50da324d0011135ca7e1c9885acd @@ -0,0 +1,77 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + newNode.next = head; + head = newNode; + } + size++; + } + + public E removeFirst() { + if (head == null) throw new NoSuchElementException(); + E tmp; + if (size == 1) { + tmp = head.data; + head = tail = null; + } else { + + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3c/a072006041490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3c/a072006041490011162d85db97d6efcd deleted file mode 100644 index 663f5c1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3c/a072006041490011162d85db97d6efcd +++ /dev/null @@ -1,340 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3c/f0e216ee3e4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/3c/f0e216ee3e4600111c1cd5b6f02d115c deleted file mode 100644 index a18f6de..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3c/f0e216ee3e4600111c1cd5b6f02d115c +++ /dev/null @@ -1,13 +0,0 @@ -package vettore_ordinabile; - -public class VettoriIntero extends VettoreOrdinabile{ - - // Metodo costruttore default - /* - * Contiene solamente 10 elementi - * */ - public VettoriIntero() { - super(10); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/306db8809e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/306db8809e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..4c4e01e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/306db8809e4c0011135ca7e1c9885acd @@ -0,0 +1,47 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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. + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/308c490330470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/308c490330470011162d85db97d6efcd deleted file mode 100644 index 83a2cad..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/308c490330470011162d85db97d6efcd +++ /dev/null @@ -1,139 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - preorder(root, temporaryList); - return temporaryList.iterator(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/809402126b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/809402126b460011162d85db97d6efcd deleted file mode 100644 index a997bec..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/809402126b460011162d85db97d6efcd +++ /dev/null @@ -1,103 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode oldParent = parent; - parent = null; - - return oldParent; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/d0cc27995b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/d0cc27995b490011162d85db97d6efcd deleted file mode 100644 index 63421f2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/d0cc27995b490011162d85db97d6efcd +++ /dev/null @@ -1,446 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - root = new BinaryNode(); - BinaryNode currentNode = root; - E currentObject = iterator.next(); - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3e/401344053c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/3e/401344053c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..4c61cf6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3e/401344053c4d0011135ca7e1c9885acd @@ -0,0 +1,163 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node nextNode = prevNode.next; + + prevNode.next = newNode; + newNode.prev = prevNode; + newNode.next = nextNode; + nextNode.prev = newNode; + + size++; + } + + public E get(int index) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3e/603873727e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/3e/603873727e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..c88a27d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3e/603873727e4c00111d9cbe34e2b6027a @@ -0,0 +1,48 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + for () { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9d/10ef3bca66460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3e/90898c119f4c0011135ca7e1c9885acd similarity index 100% rename from .metadata/.plugins/org.eclipse.core.resources/.history/9d/10ef3bca66460011162d85db97d6efcd rename to .metadata/.plugins/org.eclipse.core.resources/.history/3e/90898c119f4c0011135ca7e1c9885acd diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3e/a0183c26aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/3e/a0183c26aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..0c9e335 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3e/a0183c26aa4c0011135ca7e1c9885acd @@ -0,0 +1,24 @@ +package jcf_map.exercise; + +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, ) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3e/a056b166ac4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/3e/a056b166ac4c0011135ca7e1c9885acd new file mode 100644 index 0000000..131ba55 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3e/a056b166ac4c0011135ca7e1c9885acd @@ -0,0 +1,42 @@ +package jcf_map.exercise; + +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 int ContaDuplicati(BinaryNode node) { + int dup = 0; + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + + for () { + + } + + return dup; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3f/4059500f3e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3f/4059500f3e490011162d85db97d6efcd deleted file mode 100644 index 9627d1a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3f/4059500f3e490011162d85db97d6efcd +++ /dev/null @@ -1,288 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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. - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3f/504df4ec2d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3f/504df4ec2d470011162d85db97d6efcd deleted file mode 100644 index 54f84c9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3f/504df4ec2d470011162d85db97d6efcd +++ /dev/null @@ -1,111 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - if (temp != null) { - temp.setData(null); - return true; - } - return false; - } - - public boolean contains(E targetElement) { - return find(targetElement, root) != null; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3f/f05f9e933f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/3f/f05f9e933f4600111c1cd5b6f02d115c deleted file mode 100644 index 114910c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3f/f05f9e933f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,50 +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 - */ - @Override - public boolean aggiungi(Object object) { - return false; - } - - // Metodo aggiungi - /* - * Questo metodo aggiunge realmente l'elemento di tipo - * Integer - */ - public boolean aggiungi(Integer integer) { - return super.aggiungi(integer); - } - - // Metodo ordina - /* - * Questo metodo permette il funzionamento dell'ordinamento - * tramite comparatore simulato - */ - @Override - public int ordina(Object elemento1, Object elemento2) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4/30d728f0374600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/4/30d728f0374600111c1cd5b6f02d115c deleted file mode 100644 index a341225..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4/30d728f0374600111c1cd5b6f02d115c +++ /dev/null @@ -1,45 +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; - } - - /** - * 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; - } - - /** - * Restituisce la dimensione corrente - */ - public int dimensione() { - return dimensioneCorrente; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4/60ac45bf31470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4/60ac45bf31470011162d85db97d6efcd deleted file mode 100644 index ff09d3e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4/60ac45bf31470011162d85db97d6efcd +++ /dev/null @@ -1,185 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4/d09883cc7f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/4/d09883cc7f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..0d460da --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4/d09883cc7f4c00111d9cbe34e2b6027a @@ -0,0 +1,65 @@ +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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(new Comparator() { + + }); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4/f0ae342069460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4/f0ae342069460011162d85db97d6efcd deleted file mode 100644 index 4d29975..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4/f0ae342069460011162d85db97d6efcd +++ /dev/null @@ -1,74 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/40/90e6be0e3c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/40/90e6be0e3c4600111c1cd5b6f02d115c deleted file mode 100644 index d0927ab..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/40/90e6be0e3c4600111c1cd5b6f02d115c +++ /dev/null @@ -1,67 +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 ordina l'array - */ - public void ordina() { - for (int i = 0; i < dimensioneCorrente; i++) { - int minimo = i; - for (int j = i+1; j < dimensioneCorrente; j++) { - if () - if (vettore[minimo] > vettore[j]) minimo = j; - } - - } - } - - // FUNZIONI ASTRATTE - - /** - * Funzione che restituisce il valore della comparazione - */ - protected abstract int confronta(Object elemento1, Object elemento2); -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/40/b034c1e3a94c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/40/b034c1e3a94c0011135ca7e1c9885acd new file mode 100644 index 0000000..060f81e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/40/b034c1e3a94c0011135ca7e1c9885acd @@ -0,0 +1,20 @@ +package jcf_map.exercise; + +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 Map ContaDuplicati(BinaryNode node) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/40/c0d0bd0c1e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/40/c0d0bd0c1e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..5ef1a74 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/40/c0d0bd0c1e4d0011135ca7e1c9885acd @@ -0,0 +1,7 @@ +package list.mylinkedlist; + +public class MyLinkedList { + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/41/903f77fb6a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/41/903f77fb6a460011162d85db97d6efcd deleted file mode 100644 index c556c40..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/41/903f77fb6a460011162d85db97d6efcd +++ /dev/null @@ -1,101 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode oldParent = parent; - parent = null; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/41/a01333795e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/41/a01333795e490011162d85db97d6efcd deleted file mode 100644 index 7a52b11..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/41/a01333795e490011162d85db97d6efcd +++ /dev/null @@ -1,490 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 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 node) { - if (node == null) return 0; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - int odd = 1; - - return n - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/41/a0ea3b929e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/41/a0ea3b929e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..ff97deb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/41/a0ea3b929e4c0011135ca7e1c9885acd @@ -0,0 +1,48 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/41/a0fae8d0304600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/41/a0fae8d0304600111c1cd5b6f02d115c deleted file mode 100644 index 8e361f2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/41/a0fae8d0304600111c1cd5b6f02d115c +++ /dev/null @@ -1,15 +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() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/41/e092d7263a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/41/e092d7263a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..0979822 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/41/e092d7263a4d0011135ca7e1c9885acd @@ -0,0 +1,142 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/41/e0e53463034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/41/e0e53463034c00111d9cbe34e2b6027a deleted file mode 100644 index 34f7790..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/41/e0e53463034c00111d9cbe34e2b6027a +++ /dev/null @@ -1,40 +0,0 @@ -package jcf_set.exercise; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; - -public class IteratoreSenzaDuplicati { - - public static void main(String[] main) { - new IteratoreSenzaDuplicati().run(); - } - - public void run() { - - List lista = new ArrayList(); - - 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 Iterator getIteratorNoDuplicates(Iterator it) { - HashSet tmp = new HashSet(); - while () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/42/3085eb8bac4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/42/3085eb8bac4c0011135ca7e1c9885acd new file mode 100644 index 0000000..a0db433 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/42/3085eb8bac4c0011135ca7e1c9885acd @@ -0,0 +1,43 @@ +package jcf_map.exercise; + +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 int ContaDuplicati(BinaryNode node) { + int dup = 0; + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + + for (T element : mappa.keySet()) { + int occurrency = mappa.get(element); + if (occurrency > 1) + } + + return dup; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/42/50cddb2d8f4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/42/50cddb2d8f4c0011135ca7e1c9885acd new file mode 100644 index 0000000..900cc71 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/42/50cddb2d8f4c0011135ca7e1c9885acd @@ -0,0 +1,16 @@ +package jcf_map.exercise; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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; + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/42/60a9a477a94c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/42/60a9a477a94c0011135ca7e1c9885acd new file mode 100644 index 0000000..e62b7dd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/42/60a9a477a94c0011135ca7e1c9885acd @@ -0,0 +1,13 @@ +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. + * 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) + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/42/e021a6913e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/42/e021a6913e490011162d85db97d6efcd deleted file mode 100644 index 485dfc6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/42/e021a6913e490011162d85db97d6efcd +++ /dev/null @@ -1,300 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/42/e0cbea023a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/42/e0cbea023a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..b66d5ae --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/42/e0cbea023a4d0011135ca7e1c9885acd @@ -0,0 +1,136 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/43/1001729a50490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/43/1001729a50490011162d85db97d6efcd deleted file mode 100644 index b6c976c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/43/1001729a50490011162d85db97d6efcd +++ /dev/null @@ -1,409 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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. - */ - public LinkedBinaryTree(List ) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/43/60a06d70aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/43/60a06d70aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..031410c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/43/60a06d70aa4c0011135ca7e1c9885acd @@ -0,0 +1,30 @@ +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + if (node == null) return mappa; + + if (node.getLeft() != null) ContaDuplicati(node.getLeft(), mappa); + if (node.getRight() != null) ContaDuplicati(node.getRight(), mappa); + + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/43/c0ba9c305d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/43/c0ba9c305d490011162d85db97d6efcd deleted file mode 100644 index 39b579c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/43/c0ba9c305d490011162d85db97d6efcd +++ /dev/null @@ -1,474 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/43/d0bafae776460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/43/d0bafae776460011162d85db97d6efcd deleted file mode 100644 index 4fed0c4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/43/d0bafae776460011162d85db97d6efcd +++ /dev/null @@ -1,147 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - - while (cur.parent != null){ - d++; - cur = cur.parent; - } - - return d; - } - - public int height() { - if (left == null && right == null) return 0; - - int hLeft = (left == null) ? 0 : left.height(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/44/3010e77730470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/44/3010e77730470011162d85db97d6efcd deleted file mode 100644 index f75073a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/44/3010e77730470011162d85db97d6efcd +++ /dev/null @@ -1,154 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/44/a0094e0c68460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/44/a0094e0c68460011162d85db97d6efcd deleted file mode 100644 index 9fd9084..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/44/a0094e0c68460011162d85db97d6efcd +++ /dev/null @@ -1,52 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode getParent() { - return parent; - } - - public boolean isRoot() { - return parent==null; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/44/a04603031e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/44/a04603031e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..b2ee34c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/44/a04603031e4d0011135ca7e1c9885acd @@ -0,0 +1,5 @@ +package list.mylinkedlist; + +public class MyLinkedList { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/44/e0acee9633490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/44/e0acee9633490011162d85db97d6efcd deleted file mode 100644 index ceb0aeb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/44/e0acee9633490011162d85db97d6efcd +++ /dev/null @@ -1,257 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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. - */ - protected void itpreorder(BinaryNode node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - if - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/44/f0005c81324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/44/f0005c81324d0011135ca7e1c9885acd new file mode 100644 index 0000000..f975633 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/44/f0005c81324d0011135ca7e1c9885acd @@ -0,0 +1,67 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + newNode.next = head; + head = newNode; + } + size++; + } + + public E removeFirst() { + if (head == null) throw new NoSuchElementException(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/45/804a13718f4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/45/804a13718f4c0011135ca7e1c9885acd new file mode 100644 index 0000000..2d9cdae --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/45/804a13718f4c0011135ca7e1c9885acd @@ -0,0 +1,23 @@ +package jcf_map.exercise; + +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/45/9074d68c42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/45/9074d68c42490011162d85db97d6efcd deleted file mode 100644 index 417e270..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/45/9074d68c42490011162d85db97d6efcd +++ /dev/null @@ -1,361 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - Boolean flag; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/45/d0987f61314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/45/d0987f61314600111c1cd5b6f02d115c deleted file mode 100644 index 6746395..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/45/d0987f61314600111c1cd5b6f02d115c +++ /dev/null @@ -1,23 +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; - } - - /** - * Funzione aggiungi - */ - public boolean aggiungi(Object elemento) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/45/f08884f241490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/45/f08884f241490011162d85db97d6efcd deleted file mode 100644 index fcb3ac2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/45/f08884f241490011162d85db97d6efcd +++ /dev/null @@ -1,344 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - itinorder(root, temporaryList); - return temporaryList.iterator(); - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/46/10aa9897084c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/46/10aa9897084c00111d9cbe34e2b6027a deleted file mode 100644 index 35c6d63..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/46/10aa9897084c00111d9cbe34e2b6027a +++ /dev/null @@ -1,16 +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}; - Integer[] bArray = {1, 5, -6, -4, 5}; - - // Input come HashSet (Ordine non garantito) - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/46/30e0646267460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/46/30e0646267460011162d85db97d6efcd deleted file mode 100644 index 8c0556a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/46/30e0646267460011162d85db97d6efcd +++ /dev/null @@ -1,25 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode right) { - - } - - // METODI - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/46/60e18a5a034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/46/60e18a5a034c00111d9cbe34e2b6027a deleted file mode 100644 index 39898b7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/46/60e18a5a034c00111d9cbe34e2b6027a +++ /dev/null @@ -1,40 +0,0 @@ -package jcf_set.exercise; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; - -public class IteratoreSenzaDuplicati { - - public static void main(String[] main) { - new IteratoreSenzaDuplicati().run(); - } - - public void run() { - - List lista = new ArrayList(); - - 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 Iterator getIteratorNoDuplicates(Iterator it) { - HashSet tmp = new HashSet(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/46/9051198a5c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/46/9051198a5c490011162d85db97d6efcd deleted file mode 100644 index 292612f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/46/9051198a5c490011162d85db97d6efcd +++ /dev/null @@ -1,459 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/46/c0b9549e404600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/46/c0b9549e404600111c1cd5b6f02d115c deleted file mode 100644 index 1d72ec5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/46/c0b9549e404600111c1cd5b6f02d115c +++ /dev/null @@ -1,19 +0,0 @@ -package vettore_ordinabile; - -public class Main { - - public static void main (String[] Args) { - - System.out.println("\nTest VettoreIntero"); - Integer i1 = Integer.valueOf(3); - Integer i2 = Integer.valueOf(389); - Integer i3 = Integer.valueOf(15); - Integer i4 = Integer.valueOf(10); - - VettoriIntero VI = new VettoriIntero(4); - - VI.aggiungi(i1); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/47/d00332e63c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/47/d00332e63c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..5d5e1e1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/47/d00332e63c4d0011135ca7e1c9885acd @@ -0,0 +1,180 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + return currentNode.data; + } + + public E set(int index, E item) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + + Node currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + E oldData = currentNode.data; + currentNode.data = item; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/47/e0aca0e534470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/47/e0aca0e534470011162d85db97d6efcd deleted file mode 100644 index c97e1ac..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/47/e0aca0e534470011162d85db97d6efcd +++ /dev/null @@ -1,222 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - - queueOfNodes.add(node); - - while() { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/48/105698ea3a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/48/105698ea3a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..73a5891 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/48/105698ea3a4d0011135ca7e1c9885acd @@ -0,0 +1,147 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/48/5019b48d324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/48/5019b48d324d0011135ca7e1c9885acd new file mode 100644 index 0000000..45c9aeb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/48/5019b48d324d0011135ca7e1c9885acd @@ -0,0 +1,69 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + newNode.next = head; + head = newNode; + } + size++; + } + + public E removeFirst() { + if (head == null) throw new NoSuchElementException(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/48/e0be034d034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/48/e0be034d034c00111d9cbe34e2b6027a deleted file mode 100644 index 509cf71..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/48/e0be034d034c00111d9cbe34e2b6027a +++ /dev/null @@ -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 lista = new ArrayList(); - - 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 Iterator getIteratorNoDuplicates(Iterator it) { - HashSet tmp = new HashSet(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/48/f0d96ef466460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/48/f0d96ef466460011162d85db97d6efcd deleted file mode 100644 index 3545952..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/48/f0d96ef466460011162d85db97d6efcd +++ /dev/null @@ -1,10 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/49/401ca6de67460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/49/401ca6de67460011162d85db97d6efcd deleted file mode 100644 index 5ecba6c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/49/401ca6de67460011162d85db97d6efcd +++ /dev/null @@ -1,40 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/49/d05f54f2344d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/49/d05f54f2344d0011135ca7e1c9885acd new file mode 100644 index 0000000..99167aa --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/49/d05f54f2344d0011135ca7e1c9885acd @@ -0,0 +1,80 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node secondPlace = head.next; + + head = secondPlace; + + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4a/60c6c75b804c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/4a/60c6c75b804c00111d9cbe34e2b6027a new file mode 100644 index 0000000..cb62505 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4a/60c6c75b804c00111d9cbe34e2b6027a @@ -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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(archivio); + return archivioOrdinato; + } + + // 6. Stampa su video l'archivio attuale + public void stampaArchivio() { + System.out.println(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4a/90995109aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/4a/90995109aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..76c327d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4a/90995109aa4c0011135ca7e1c9885acd @@ -0,0 +1,24 @@ +package jcf_map.exercise; + +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4a/b061b799314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/4a/b061b799314600111c1cd5b6f02d115c deleted file mode 100644 index aa514cc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4a/b061b799314600111c1cd5b6f02d115c +++ /dev/null @@ -1,26 +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; - } - - /** - * Funzione aggiungi - */ - public boolean aggiungi(Object elemento) { - if (elemento != null && dimensioneCorrente < dimensioneMassima) { - - return true; - } else return false; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4b/20ebf39e3f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/20ebf39e3f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..410c86a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/20ebf39e3f4d0011135ca7e1c9885acd @@ -0,0 +1,181 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + return currentNode.data; + } + + public E set(int index, E item) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + + Node currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + E oldData = currentNode.data; + currentNode.data = item; + return oldData; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4b/30407c1938470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/30407c1938470011162d85db97d6efcd deleted file mode 100644 index a183669..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4b/30407c1938470011162d85db97d6efcd +++ /dev/null @@ -1,235 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4b/40083a7a2d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/40083a7a2d470011162d85db97d6efcd deleted file mode 100644 index 3bc988a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4b/40083a7a2d470011162d85db97d6efcd +++ /dev/null @@ -1,109 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - if (temp != null) { - temp.setData(null); - return true; - } - return false; - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4b/801e556f394d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/801e556f394d0011135ca7e1c9885acd new file mode 100644 index 0000000..abc5aff --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/801e556f394d0011135ca7e1c9885acd @@ -0,0 +1,132 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4b/8045cd9a374d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/8045cd9a374d0011135ca7e1c9885acd new file mode 100644 index 0000000..a18c0b0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/8045cd9a374d0011135ca7e1c9885acd @@ -0,0 +1,113 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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(); + if (size == 0) { + head = tail = new Node(item, null, null); + } else { + Node newNode = new Node(); + } + size++; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4b/d032b0536a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/d032b0536a460011162d85db97d6efcd deleted file mode 100644 index 9ef29e3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4b/d032b0536a460011162d85db97d6efcd +++ /dev/null @@ -1,80 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (this.parent == null) return null; - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4c/7083ebe876460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4c/7083ebe876460011162d85db97d6efcd deleted file mode 100644 index 9e73805..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4c/7083ebe876460011162d85db97d6efcd +++ /dev/null @@ -1,147 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - - while (cur.parent != null){ - d++; - cur = cur.parent; - } - - return d; - } - - public int height() { - if (left == null && right == null) return 0; - - int hLeft = (left == null) ? 0 : left.height(); - int hRight = (right == null) ? 0 : right.height(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4c/d09a61472f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4c/d09a61472f470011162d85db97d6efcd deleted file mode 100644 index 04941da..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4c/d09a61472f470011162d85db97d6efcd +++ /dev/null @@ -1,127 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4d/10e536e03e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4d/10e536e03e490011162d85db97d6efcd deleted file mode 100644 index 6dcf239..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4d/10e536e03e490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - if (current.getRight()) - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4d/301d8c89034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/4d/301d8c89034c00111d9cbe34e2b6027a deleted file mode 100644 index 94e904b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4d/301d8c89034c00111d9cbe34e2b6027a +++ /dev/null @@ -1,47 +0,0 @@ -package jcf_set.exercise; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; - -public class IteratoreSenzaDuplicati { - - public static void main(String[] main) { - new IteratoreSenzaDuplicati().run(); - } - - public void run() { - - List lista = new ArrayList(); - - 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 Iterator getIteratorNoDuplicates(Iterator it) { - HashSet tmp = new HashSet(); - while (it.hasNext()) tmp.add(it.next()); - return tmp.iterator(); - } - - // Esercizio 2 - /* - * Passare come parametro una lista con duplicati. - * Ottenere un iteratore senza duplicati e ordinato. - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4d/c0774f6c094c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/4d/c0774f6c094c00111d9cbe34e2b6027a deleted file mode 100644 index 64d5bfe..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4d/c0774f6c094c00111d9cbe34e2b6027a +++ /dev/null @@ -1,34 +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 aHashSet = new HashSet(Arrays.asList(aArray)); - Set bHashSet = new HashSet(Arrays.asList(bArray)); - - - - } - - // Operazioni di insiemistica - - /* - * Per eseguire una unione, è conveniente - * adoperare la Set poichè l'implementazione - * TreeSet permette di non mantenere duplicati. - */ - public static > Set union () { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4d/c0880dc5354d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/4d/c0880dc5354d0011135ca7e1c9885acd new file mode 100644 index 0000000..d963519 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4d/c0880dc5354d0011135ca7e1c9885acd @@ -0,0 +1,90 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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) return new NoSuchElementException(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4e/40f1c63c2c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4e/40f1c63c2c470011162d85db97d6efcd deleted file mode 100644 index 9587897..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4e/40f1c63c2c470011162d85db97d6efcd +++ /dev/null @@ -1,95 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode resNode; - resNode = find(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4e/601cd7aa33490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4e/601cd7aa33490011162d85db97d6efcd deleted file mode 100644 index 255d941..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4e/601cd7aa33490011162d85db97d6efcd +++ /dev/null @@ -1,268 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 il primo 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - if - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/609e16c965460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/609e16c965460011162d85db97d6efcd deleted file mode 100644 index f1c2ec3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/609e16c965460011162d85db97d6efcd +++ /dev/null @@ -1,19 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public interface BinaryTree { - - public boolean isEmpty(); - - public int size(); - - public void clear(); - - public E getRoot(); - - public boolean contains(E targetElement); - - public Iterator - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/60f31582384d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/60f31582384d0011135ca7e1c9885acd new file mode 100644 index 0000000..902662c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/60f31582384d0011135ca7e1c9885acd @@ -0,0 +1,117 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node oldLastNode = tail; + oldLastNode.next = newNode; + newNode.prev = oldLastNode; + tail = newNode; + } + size++; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/9083c736304d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/9083c736304d0011135ca7e1c9885acd new file mode 100644 index 0000000..abb3deb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/9083c736304d0011135ca7e1c9885acd @@ -0,0 +1,59 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(null, null, item); + } else { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/a08872953d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/a08872953d490011162d85db97d6efcd deleted file mode 100644 index 39f7cfd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/a08872953d490011162d85db97d6efcd +++ /dev/null @@ -1,282 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - itpreorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATOR - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/d0e5fa51094c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/d0e5fa51094c00111d9cbe34e2b6027a deleted file mode 100644 index 395e92e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/d0e5fa51094c00111d9cbe34e2b6027a +++ /dev/null @@ -1,32 +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 aHashSet = new HashSet(Arrays.asList(aArray)); - Set bHashSet = new HashSet(Arrays.asList(bArray)); - - - - } - - // Operazioni di insiemistica - - /* - * Per eseguire una unione, è conveniente - * adoperare la Set poichè l'implementazione - * TreeSet permette di non mantenere duplicati. - */ - public static - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/f05965a1374d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/f05965a1374d0011135ca7e1c9885acd new file mode 100644 index 0000000..6a1d0da --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/f05965a1374d0011135ca7e1c9885acd @@ -0,0 +1,114 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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(); + if (size == 0) { + head = tail = new Node(item, null, null); + } else { + Node newNode = new Node(); + } + size++; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5/0053573f43490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5/0053573f43490011162d85db97d6efcd deleted file mode 100644 index ee8ab62..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5/0053573f43490011162d85db97d6efcd +++ /dev/null @@ -1,384 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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()); - flags.push(false); - } - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5/908c6e4e5d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5/908c6e4e5d490011162d85db97d6efcd deleted file mode 100644 index 45f4b3f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5/908c6e4e5d490011162d85db97d6efcd +++ /dev/null @@ -1,481 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public void numberLeaf() { - - } - - public void numberLeaf(BinaryNode node) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5/d00d46ae384d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/5/d00d46ae384d0011135ca7e1c9885acd new file mode 100644 index 0000000..655dc1d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5/d00d46ae384d0011135ca7e1c9885acd @@ -0,0 +1,119 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node oldLastNode = tail; + oldLastNode.next = newNode; + newNode.prev = oldLastNode; + tail = newNode; + } + size++; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/50/20557e24804c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/50/20557e24804c00111d9cbe34e2b6027a new file mode 100644 index 0000000..4b4f13b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/50/20557e24804c00111d9cbe34e2b6027a @@ -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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(new Comparator() { + @Override + public int compare(String s1, String s2) { + return s1.compareTo(s2); + } + }); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/50/301fd41376460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/50/301fd41376460011162d85db97d6efcd deleted file mode 100644 index 4f06d18..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/50/301fd41376460011162d85db97d6efcd +++ /dev/null @@ -1,127 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/50/6014e5aa5a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/50/6014e5aa5a490011162d85db97d6efcd deleted file mode 100644 index a1e5ef6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/50/6014e5aa5a490011162d85db97d6efcd +++ /dev/null @@ -1,444 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(); - - if (wing == 0) { - // Left Wing - - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/50/703706531e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/50/703706531e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..227720a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/50/703706531e4d0011135ca7e1c9885acd @@ -0,0 +1,17 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Data + private E data; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/50/7055109f5d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/50/7055109f5d490011162d85db97d6efcd deleted file mode 100644 index 494173c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/50/7055109f5d490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public void numberLeaf() { - Integer number = 0; - numberLeaf(root, number); - } - - public void numberLeaf(BinaryNode node, Integer number) { - if (node == null) return; - - if (node.getLeft() == null && node.getRight() == null) number++; - else { - numberLeaf(node.getLeft(), number); - numberLeaf(); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/50/d0ac635b2f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/50/d0ac635b2f470011162d85db97d6efcd deleted file mode 100644 index b51e27d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/50/d0ac635b2f470011162d85db97d6efcd +++ /dev/null @@ -1,129 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - temporaryList.add(node.getData()); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/51/004e91c067460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/51/004e91c067460011162d85db97d6efcd deleted file mode 100644 index 530be36..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/51/004e91c067460011162d85db97d6efcd +++ /dev/null @@ -1,34 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/51/1045b4fbab4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/51/1045b4fbab4c0011135ca7e1c9885acd new file mode 100644 index 0000000..9863e32 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/51/1045b4fbab4c0011135ca7e1c9885acd @@ -0,0 +1,39 @@ +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 Map ContaDuplicati(BinaryNode node) { + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + return mappa; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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); + + return mappa; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/51/80dfe9df6a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/51/80dfe9df6a460011162d85db97d6efcd deleted file mode 100644 index a6d574e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/51/80dfe9df6a460011162d85db97d6efcd +++ /dev/null @@ -1,94 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/51/9076b8502e470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/51/9076b8502e470011162d85db97d6efcd deleted file mode 100644 index 081dbbb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/51/9076b8502e470011162d85db97d6efcd +++ /dev/null @@ -1,117 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - if (temp != null) { - temp.setData(null); - return true; - } - return false; - } - - public boolean contains(E targetElement) { - return find(targetElement, root) != null; - } - - // ITERATORI - - /* - * Qui si utilizza il metodo del preorder - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/51/a0c1b4ba1b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/51/a0c1b4ba1b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..3b74181 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/51/a0c1b4ba1b4d0011135ca7e1c9885acd @@ -0,0 +1,40 @@ +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 + */ + + public static void main(String[] main) { + new CompanyMap().run(); + } + + public void run() { + TreeMap mappaAssociazione = new TreeMap(); + + mappaAssociazione.put("Rossi Marco", 8); + mappaAssociazione.put("Bianchi Luca", 14); + mappaAssociazione.put("Esposito Andrea", 6); + mappaAssociazione.put("Ferrari Matteo", 6); + mappaAssociazione.put("Romano Giulia", 14); + mappaAssociazione.put("Ricci Alessia", 6); + + System.out.println("Stampa mappa associazione: "); + System.out.println(mappaAssociazione); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/51/d037f2ec59490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/51/d037f2ec59490011162d85db97d6efcd deleted file mode 100644 index c2ca817..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/51/d037f2ec59490011162d85db97d6efcd +++ /dev/null @@ -1,434 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - BinaryNode currentNode = root; - - while (iterator.hasNext()) { - E currentObject = iterator.next(); - - - currentNode.setData(currentObject); - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/52/00466e563c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/52/00466e563c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..9380c69 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/52/00466e563c4d0011135ca7e1c9885acd @@ -0,0 +1,168 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for (int i = 0; i < size; i++) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/52/1076515aac4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/52/1076515aac4c0011135ca7e1c9885acd new file mode 100644 index 0000000..699408b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/52/1076515aac4c0011135ca7e1c9885acd @@ -0,0 +1,40 @@ +package jcf_map.exercise; + +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 int ContaDuplicati(BinaryNode node) { + int dup = 0; + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + + + + return dup; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/52/20cb337d1e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/52/20cb337d1e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..29d369d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/52/20cb337d1e4d0011135ca7e1c9885acd @@ -0,0 +1,26 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Data + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/52/90cb57c368460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/52/90cb57c368460011162d85db97d6efcd deleted file mode 100644 index dc2df44..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/52/90cb57c368460011162d85db97d6efcd +++ /dev/null @@ -1,64 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/52/a055924468460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/52/a055924468460011162d85db97d6efcd deleted file mode 100644 index a07ccfe..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/52/a055924468460011162d85db97d6efcd +++ /dev/null @@ -1,56 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode getParent() { - return parent; - } - - public boolean isRoot() { - return parent == null; - } - - public boolean isLeftChild() { - if (parent != null && parent.getLeft() == this) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/52/b08162f142490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/52/b08162f142490011162d85db97d6efcd deleted file mode 100644 index 5b75c38..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/52/b08162f142490011162d85db97d6efcd +++ /dev/null @@ -1,374 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - Boolean flag; - - queueOfNodes.push(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - flag = flags.pop(); - - if (flag) { - // il nodo è da visitare - - } else { - // il nodo non è ancora da visitare - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/52/f08178a51b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/52/f08178a51b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..c35efee --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/52/f08178a51b4d0011135ca7e1c9885acd @@ -0,0 +1,36 @@ +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 + */ + + public void run() { + TreeMap mappaAssociazione = new TreeMap(); + + mappaAssociazione.put("Rossi Marco", 8); + mappaAssociazione.put("Bianchi Luca", 14); + mappaAssociazione.put("Esposito Andrea", 6); + mappaAssociazione.put("Ferrari Matteo", 6); + mappaAssociazione.put("Romano Giulia", 14); + mappaAssociazione.put("Ricci Alessia", 6); + + System.out.println("Stampa mappa associazione: "); + System.out.println(mappaAssociazione); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/53/20bbb9a5404600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/53/20bbb9a5404600111c1cd5b6f02d115c deleted file mode 100644 index be6de4f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/53/20bbb9a5404600111c1cd5b6f02d115c +++ /dev/null @@ -1,22 +0,0 @@ -package vettore_ordinabile; - -public class Main { - - public static void main (String[] Args) { - - System.out.println("\nTest VettoreIntero"); - Integer i1 = Integer.valueOf(3); - Integer i2 = Integer.valueOf(389); - Integer i3 = Integer.valueOf(15); - Integer i4 = Integer.valueOf(10); - - VettoriIntero VI = new VettoriIntero(4); - - VI.aggiungi(i1); - VI.aggiungi(i2); - VI.aggiungi(i3); - VI.aggiungi(i4); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/53/a0ad6986aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/53/a0ad6986aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..941c64f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/53/a0ad6986aa4c0011135ca7e1c9885acd @@ -0,0 +1,32 @@ +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap 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(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/53/f000f8983f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/53/f000f8983f4600111c1cd5b6f02d115c deleted file mode 100644 index 73f9fc2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/53/f000f8983f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,50 +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 - */ - @Override - public boolean aggiungi(Object object) { - return false; - } - - // Metodo aggiungi - /* - * Questo metodo aggiunge realmente l'elemento di tipo - * Integer - */ - public boolean aggiungi(Integer integer) { - return super.aggiungi(integer); - } - - // Metodo ordina - /* - * Questo metodo permette il funzionamento dell'ordinamento - * tramite comparatore simulato - */ - @Override - public int ordina(Object elemento1, Object elemento2) { - Integer i1 = (Integer)elemento1; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/54/000e1e4cac4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/54/000e1e4cac4c0011135ca7e1c9885acd new file mode 100644 index 0000000..57fc95a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/54/000e1e4cac4c0011135ca7e1c9885acd @@ -0,0 +1,36 @@ +package jcf_map.exercise; + +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 int ContaDuplicati(BinaryNode node) { + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + return mappa; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/54/0076262b68460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/54/0076262b68460011162d85db97d6efcd deleted file mode 100644 index 8795bb3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/54/0076262b68460011162d85db97d6efcd +++ /dev/null @@ -1,56 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode getParent() { - return parent; - } - - public boolean isRoot() { - return parent == null; - } - - public boolean isLeftChild() { - if () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/54/2006ffc076460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/54/2006ffc076460011162d85db97d6efcd deleted file mode 100644 index 15b6bed..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/54/2006ffc076460011162d85db97d6efcd +++ /dev/null @@ -1,145 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - - while (cur.parent != null){ - d++; - cur = cur.parent; - } - - return d; - } - - public int height() { - if (left == null && right == null) return 0; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/54/20cba89730470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/54/20cba89730470011162d85db97d6efcd deleted file mode 100644 index 5a01ac7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/54/20cba89730470011162d85db97d6efcd +++ /dev/null @@ -1,159 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/54/d03231a7394d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/54/d03231a7394d0011135ca7e1c9885acd new file mode 100644 index 0000000..5a9410b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/54/d03231a7394d0011135ca7e1c9885acd @@ -0,0 +1,134 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/55/204b0787314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/55/204b0787314600111c1cd5b6f02d115c deleted file mode 100644 index 32d6042..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/55/204b0787314600111c1cd5b6f02d115c +++ /dev/null @@ -1,23 +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; - } - - /** - * Funzione aggiungi - */ - public boolean aggiungi(Object elemento) { - if () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/55/300820856b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/55/300820856b460011162d85db97d6efcd deleted file mode 100644 index 1678a21..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/55/300820856b460011162d85db97d6efcd +++ /dev/null @@ -1,110 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode oldParent = parent; - parent = null; - - if (oldParent.left == this) oldParent.left = null; - else oldParent.right = null; - - return oldParent; - } - - public boolean hasLeft() { - return left == null; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/55/a069e620364d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/55/a069e620364d0011135ca7e1c9885acd new file mode 100644 index 0000000..eff3716 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/55/a069e620364d0011135ca7e1c9885acd @@ -0,0 +1,96 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/56/3099355b85460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/56/3099355b85460011162d85db97d6efcd deleted file mode 100644 index e35c0db..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/56/3099355b85460011162d85db97d6efcd +++ /dev/null @@ -1,72 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - return leftTree; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/56/70654bb1024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/56/70654bb1024c00111d9cbe34e2b6027a deleted file mode 100644 index 6b838b1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/56/70654bb1024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,17 +0,0 @@ -package jcf_set.exercise; - -import java.util.List; - -public class IteratoreSenzaDuplicati { - - public static void main(String[] main) { - new IteratoreSenzaDuplicati().run(); - } - - public void run() { - - List lista = - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/56/8046d3d032470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/56/8046d3d032470011162d85db97d6efcd deleted file mode 100644 index 90946ba..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/56/8046d3d032470011162d85db97d6efcd +++ /dev/null @@ -1,191 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - postorder(root, temporaryList); - return temporaryList.iterator(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/56/a019c8b976460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/56/a019c8b976460011162d85db97d6efcd deleted file mode 100644 index 43763ed..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/56/a019c8b976460011162d85db97d6efcd +++ /dev/null @@ -1,144 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - - while (cur.parent != null){ - d++; - cur = cur.parent; - } - - return d; - } - - public int height() { - if (left == null && right == null) return 0; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/57/000e14079c4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/57/000e14079c4c0011135ca7e1c9885acd new file mode 100644 index 0000000..bd49198 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/57/000e14079c4c0011135ca7e1c9885acd @@ -0,0 +1,36 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + if (myList.isEmpty()) return true; + // Verifica che la lista si pari o dispari + HashMap valori = new HashMap(); + + Iterator it = myList.iterator(); + while (it.hasNext()) { + E current = it.next(); + if (valori.get(current) == null) + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/57/10a5470e1e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/57/10a5470e1e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..847ef07 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/57/10a5470e1e4d0011135ca7e1c9885acd @@ -0,0 +1,9 @@ +package list.mylinkedlist; + +public class MyLinkedList { + + /* + * Classe Nodo + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/57/207d929e324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/57/207d929e324d0011135ca7e1c9885acd new file mode 100644 index 0000000..8197988 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/57/207d929e324d0011135ca7e1c9885acd @@ -0,0 +1,71 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + newNode.next = head; + head = newNode; + } + size++; + } + + public E removeFirst() { + if (head == null) throw new NoSuchElementException(); + if (size == 1) { + Node tmp = head; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/57/20e10b4768460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/57/20e10b4768460011162d85db97d6efcd deleted file mode 100644 index 0f53cce..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/57/20e10b4768460011162d85db97d6efcd +++ /dev/null @@ -1,56 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode getParent() { - return parent; - } - - public boolean isRoot() { - return parent == null; - } - - public boolean isLeftChild() { - if (parent != null && parent.getLeft() == this) return true; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/57/300ba099aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/57/300ba099aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..6dc46cd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/57/300ba099aa4c0011135ca7e1c9885acd @@ -0,0 +1,33 @@ +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, TreeMap 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 () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/57/30825eb267460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/57/30825eb267460011162d85db97d6efcd deleted file mode 100644 index 87ec9dd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/57/30825eb267460011162d85db97d6efcd +++ /dev/null @@ -1,33 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode right) { - this.data = data; - - this.left = left; - this.right = right; - - this.parent = null; - - if (this.left != null) this.left.parent = this; - } - - // METODI - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/58/00221bbc42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/58/00221bbc42490011162d85db97d6efcd deleted file mode 100644 index 9e5fe25..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/58/00221bbc42490011162d85db97d6efcd +++ /dev/null @@ -1,366 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - Boolean flag; - - queueOfNodes.push(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/58/00de8e509e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/58/00de8e509e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..e425a48 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/58/00de8e509e4c0011135ca7e1c9885acd @@ -0,0 +1,44 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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); + } + for (E chiave : valori.keySet()) { + int valore = valori.get(); + } + // Controllo short circuit di disparità + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/58/902e92d0034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/58/902e92d0034c00111d9cbe34e2b6027a deleted file mode 100644 index 06c79f1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/58/902e92d0034c00111d9cbe34e2b6027a +++ /dev/null @@ -1,50 +0,0 @@ -package jcf_set.exercise; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; - -public class IteratoreSenzaDuplicati { - - public static void main(String[] main) { - new IteratoreSenzaDuplicati().run(); - } - - public void run() { - - List lista = new ArrayList(); - - lista.add("tree"); - lista.add("flower"); - lista.add("tree"); - lista.add("flower"); - lista.add("animal"); - lista.add("flower"); - lista.add("fruit"); - - - - } - - // Esercizio 1 - /* - * Passare una lista con duplicati. - * Ottenere un iteratore privo di duplicati. - */ - private static > Iterator getIteratorNoDuplicates(Iterator it) { - HashSet tmp = new HashSet(); - while (it.hasNext()) tmp.add(it.next()); - return tmp.iterator(); - } - - // Esercizio 2 - /* - * Passare come parametro una lista con duplicati. - * Ottenere un iteratore senza duplicati e ordinato. - */ - private static > Iterator getIteratorNoDuplicatesOrdinated(Iterator it) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/58/d0f2ac5984460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/58/d0f2ac5984460011162d85db97d6efcd deleted file mode 100644 index 5302347..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/58/d0f2ac5984460011162d85db97d6efcd +++ /dev/null @@ -1,64 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/58/e0d0bd89374600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/58/e0d0bd89374600111c1cd5b6f02d115c deleted file mode 100644 index 3c233ea..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/58/e0d0bd89374600111c1cd5b6f02d115c +++ /dev/null @@ -1,36 +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; - } - - /** - * 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) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/59/20e49d4040490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/59/20e49d4040490011162d85db97d6efcd deleted file mode 100644 index cbc7431..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/59/20e49d4040490011162d85db97d6efcd +++ /dev/null @@ -1,319 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/59/606591c59e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/59/606591c59e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..bf20d57 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/59/606591c59e4c0011135ca7e1c9885acd @@ -0,0 +1,49 @@ +package jcf_map.exercise; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/59/a02ffb9334470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/59/a02ffb9334470011162d85db97d6efcd deleted file mode 100644 index 0715ce0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/59/a02ffb9334470011162d85db97d6efcd +++ /dev/null @@ -1,214 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/59/b0b9e62b034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/59/b0b9e62b034c00111d9cbe34e2b6027a deleted file mode 100644 index f36ea4f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/59/b0b9e62b034c00111d9cbe34e2b6027a +++ /dev/null @@ -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 lista = new ArrayList(); - - 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 Iterator getIteratorNoDuplicates() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/00b255bb1a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/00b255bb1a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..f99c54e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/00b255bb1a4d0011135ca7e1c9885acd @@ -0,0 +1,7 @@ +package jcf_map.exercise; + +public class CompanyMap { + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/108323e07e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/108323e07e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..81a4a64 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/108323e07e4c00111d9cbe34e2b6027a @@ -0,0 +1,50 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + for (String s : archivio.keySet()) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/20013efa9d4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/20013efa9d4c0011135ca7e1c9885acd new file mode 100644 index 0000000..91cda96 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/20013efa9d4c0011135ca7e1c9885acd @@ -0,0 +1,43 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + boolean dispari = true; + + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return dispari; + + // Verifica che la lista si pari o dispari + HashMap valori = new HashMap(); + + Iterator 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); + } + + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/201abb532c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/201abb532c470011162d85db97d6efcd deleted file mode 100644 index 559f1b1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/201abb532c470011162d85db97d6efcd +++ /dev/null @@ -1,96 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode resNode; - resNode = find(targetElement, root.getLeft()); - if (resNode == null) resNode = find(targetElement, root.getRight()); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/40ecac465d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/40ecac465d490011162d85db97d6efcd deleted file mode 100644 index 4d4657b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/40ecac465d490011162d85db97d6efcd +++ /dev/null @@ -1,481 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public void numberLeaf() { - - } - - public void printLeaf(BinaryNode node) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/601924b33c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/601924b33c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..55ac4b3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/601924b33c4d0011135ca7e1c9885acd @@ -0,0 +1,173 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + return currentNode.data; + } + + public E set(int index, E item) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/603b700d1f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/603b700d1f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..e83a01e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/603b700d1f4d0011135ca7e1c9885acd @@ -0,0 +1,30 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/70d84e8c3c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/70d84e8c3c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..850fde5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/70d84e8c3c4d0011135ca7e1c9885acd @@ -0,0 +1,169 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + return currentNode.data; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/d0656c8b59490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/d0656c8b59490011162d85db97d6efcd deleted file mode 100644 index 3df5e10..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/d0656c8b59490011162d85db97d6efcd +++ /dev/null @@ -1,429 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - - while (iterator.hasNext()) { - E current = iterator.next(); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/20d12f495e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/20d12f495e490011162d85db97d6efcd deleted file mode 100644 index 8df1313..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/20d12f495e490011162d85db97d6efcd +++ /dev/null @@ -1,487 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 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 node) { - if (node == null) return 0; - - int nLeft = (node.hasLeft() == null) ? 0 : numberLeaf(node.getLeft()); - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/306d34792f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/306d34792f470011162d85db97d6efcd deleted file mode 100644 index 63cd6c4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/306d34792f470011162d85db97d6efcd +++ /dev/null @@ -1,131 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/50b621fc5d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/50b621fc5d490011162d85db97d6efcd deleted file mode 100644 index e77e038..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/50b621fc5d490011162d85db97d6efcd +++ /dev/null @@ -1,489 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 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 node) { - if (node == null) return; - - if (node.getLeft() == null && node.getRight() == null) number++; - else { - numberLeaf(node.getLeft(), number); - numberLeaf(node.getRight(), number); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/a0652eed68460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/a0652eed68460011162d85db97d6efcd deleted file mode 100644 index 2a3278e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/a0652eed68460011162d85db97d6efcd +++ /dev/null @@ -1,64 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/b0eb910243490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/b0eb910243490011162d85db97d6efcd deleted file mode 100644 index a5a1c7c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/b0eb910243490011162d85db97d6efcd +++ /dev/null @@ -1,375 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 - if () - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/201b06733f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/201b06733f4600111c1cd5b6f02d115c deleted file mode 100644 index bb5b641..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/201b06733f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,40 +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 - */ - @Override - public boolean aggiungi(Object object) { - return false; - } - - // Metodo aggiungi - /* - * Questo metodo aggiunge realmente l'elemento di tipo - * Integer - */ - public boolean aggiungi(Integer integer) { - return super.aggiungi(integer); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/3017176541490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/3017176541490011162d85db97d6efcd deleted file mode 100644 index 3860907..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/3017176541490011162d85db97d6efcd +++ /dev/null @@ -1,341 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - itinorder(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/803c58916b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/803c58916b460011162d85db97d6efcd deleted file mode 100644 index 49cafe6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/803c58916b460011162d85db97d6efcd +++ /dev/null @@ -1,114 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/b04b230342490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/b04b230342490011162d85db97d6efcd deleted file mode 100644 index 841a595..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/b04b230342490011162d85db97d6efcd +++ /dev/null @@ -1,344 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - itinorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/f099b30242490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/f099b30242490011162d85db97d6efcd deleted file mode 100644 index fcb3ac2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/f099b30242490011162d85db97d6efcd +++ /dev/null @@ -1,344 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - itinorder(root, temporaryList); - return temporaryList.iterator(); - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/000b145d084c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/000b145d084c00111d9cbe34e2b6027a deleted file mode 100644 index 3175e50..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/000b145d084c00111d9cbe34e2b6027a +++ /dev/null @@ -1,12 +0,0 @@ -package jcf_set.exercise; - -public class Insiemistica { - - public static void main(String[] main) { - - // Insiemi di test - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/200530c93c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/200530c93c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..9389ac4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/200530c93c4d0011135ca7e1c9885acd @@ -0,0 +1,179 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + return currentNode.data; + } + + public E set(int index, E item) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + + Node currentNode = head; + E oldData; + for (int i = 0; i < index; i++) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/403c55c63b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/403c55c63b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..3a6e132 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/403c55c63b4d0011135ca7e1c9885acd @@ -0,0 +1,157 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node nextNode = prevNode.next; + + prevNode.next = newNode; + newNode.prev = prevNode; + newNode.next = nextNode; + nextNode.prev = newNode; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/40d1b9f83e4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/40d1b9f83e4600111c1cd5b6f02d115c deleted file mode 100644 index 16a8fbc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/40d1b9f83e4600111c1cd5b6f02d115c +++ /dev/null @@ -1,22 +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); - } - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/5024f02b1b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/5024f02b1b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..51756af --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/5024f02b1b4d0011135ca7e1c9885acd @@ -0,0 +1,19 @@ +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: si intende adoperare chiaramente un TreeMap. + * In modo che vi sia una associazione chiave-valore e un ordinamento basato + * sulla chiave. + * + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/a00a1b492f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/a00a1b492f470011162d85db97d6efcd deleted file mode 100644 index 400991e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/a00a1b492f470011162d85db97d6efcd +++ /dev/null @@ -1,128 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/50daf402034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/50daf402034c00111d9cbe34e2b6027a deleted file mode 100644 index a65c2a1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/50daf402034c00111d9cbe34e2b6027a +++ /dev/null @@ -1,34 +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 lista = new ArrayList(); - - 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. - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/60004df250490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/60004df250490011162d85db97d6efcd deleted file mode 100644 index 9fe5847..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/60004df250490011162d85db97d6efcd +++ /dev/null @@ -1,412 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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. - */ - /* - * - */ - public LinkedBinaryTree(List objectList) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/707621f285460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/707621f285460011162d85db97d6efcd deleted file mode 100644 index e410e4d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/707621f285460011162d85db97d6efcd +++ /dev/null @@ -1,82 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/70bf5dd042490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/70bf5dd042490011162d85db97d6efcd deleted file mode 100644 index 98b82d9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/70bf5dd042490011162d85db97d6efcd +++ /dev/null @@ -1,371 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - Boolean flag; - - queueOfNodes.push(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - flag = flags.pop(); - - if (flag) { - // il nodo è da visitare - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/a0c16d3a32490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/a0c16d3a32490011162d85db97d6efcd deleted file mode 100644 index 6ee8cdc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/a0c16d3a32490011162d85db97d6efcd +++ /dev/null @@ -1,245 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator iterator() { - return iteratorPreOrder(); - } - - // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) - - protected void itpreorder() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/d068cda06a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/d068cda06a460011162d85db97d6efcd deleted file mode 100644 index 8bdc749..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/d068cda06a460011162d85db97d6efcd +++ /dev/null @@ -1,88 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/e02bd5b0324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/e02bd5b0324d0011135ca7e1c9885acd new file mode 100644 index 0000000..53570c7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/e02bd5b0324d0011135ca7e1c9885acd @@ -0,0 +1,73 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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; + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5f/6070b0f240490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5f/6070b0f240490011162d85db97d6efcd deleted file mode 100644 index a6d6b6f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5f/6070b0f240490011162d85db97d6efcd +++ /dev/null @@ -1,332 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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) { - - } - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5f/80a5218542490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5f/80a5218542490011162d85db97d6efcd deleted file mode 100644 index f6aadca..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5f/80a5218542490011162d85db97d6efcd +++ /dev/null @@ -1,360 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5f/f063b4c13b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/5f/f063b4c13b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..42ba9dd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5f/f063b4c13b4d0011135ca7e1c9885acd @@ -0,0 +1,157 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node nextNode = prevNode.next; + + prevNode.next = newNode; + newNode.prev = prevNode; + newNode.next = nextNode; + nextNode.prev = null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6/008f04793c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/6/008f04793c4600111c1cd5b6f02d115c deleted file mode 100644 index d3155e5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6/008f04793c4600111c1cd5b6f02d115c +++ /dev/null @@ -1,77 +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; - for (int j = i+1; j < dimensioneCorrente; j++) { - if () - if (vettore[minimo] > vettore[j]) minimo = j; - } - - } - } - - // FUNZIONI ASTRATTE - - /** - * Funzione che restituisce il valore della comparazione - */ - protected abstract int confronta(Object elemento1, Object elemento2); -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6/80cd5fac5a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6/80cd5fac5a490011162d85db97d6efcd deleted file mode 100644 index 3fa0ab6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6/80cd5fac5a490011162d85db97d6efcd +++ /dev/null @@ -1,444 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode; = new BinaryNode(); - - if (wing == 0) { - // Left Wing - - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/60/40bc6eca42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/60/40bc6eca42490011162d85db97d6efcd deleted file mode 100644 index 033b17a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/60/40bc6eca42490011162d85db97d6efcd +++ /dev/null @@ -1,371 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - Boolean flag; - - queueOfNodes.push(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - flag = flags.pop(); - - if (flag) { - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/60/50acadf659490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/60/50acadf659490011162d85db97d6efcd deleted file mode 100644 index 6ac7cbd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/60/50acadf659490011162d85db97d6efcd +++ /dev/null @@ -1,437 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - BinaryNode currentNode = root; - - while (iterator.hasNext()) { - E currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - currentNode.setData(currentObject); - - if (wing == 0) { - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/60/8000b4c567460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/60/8000b4c567460011162d85db97d6efcd deleted file mode 100644 index dd43fe6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/60/8000b4c567460011162d85db97d6efcd +++ /dev/null @@ -1,36 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/60/80cc47ab394d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/60/80cc47ab394d0011135ca7e1c9885acd new file mode 100644 index 0000000..4863c28 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/60/80cc47ab394d0011135ca7e1c9885acd @@ -0,0 +1,134 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 ) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/60/d0550c75aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/60/d0550c75aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..031410c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/60/d0550c75aa4c0011135ca7e1c9885acd @@ -0,0 +1,30 @@ +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + if (node == null) return mappa; + + if (node.getLeft() != null) ContaDuplicati(node.getLeft(), mappa); + if (node.getRight() != null) ContaDuplicati(node.getRight(), mappa); + + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/60/e043dd365e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/60/e043dd365e490011162d85db97d6efcd deleted file mode 100644 index f57c5b8..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/60/e043dd365e490011162d85db97d6efcd +++ /dev/null @@ -1,486 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 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 node) { - if (node == null) return 0; - - int nLeft = (node.hasLeft() == null) ? 0 : - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/60/f0e5988d5d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/60/f0e5988d5d490011162d85db97d6efcd deleted file mode 100644 index 0f30ba5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/60/f0e5988d5d490011162d85db97d6efcd +++ /dev/null @@ -1,487 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public void numberLeaf() { - Integer number = 0; - numberLeaf(root, number); - } - - public void numberLeaf(BinaryNode node, Integer number) { - if (node == null) return; - - if (node.getLeft() == null && node.getRight() == null) number++; - else { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/61/108ccaf53a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/61/108ccaf53a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..0b6b7c3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/61/108ccaf53a4d0011135ca7e1c9885acd @@ -0,0 +1,148 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (i = 0; ) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/61/307fe6873f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/61/307fe6873f4600111c1cd5b6f02d115c deleted file mode 100644 index ed43b66..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/61/307fe6873f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,46 +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 - */ - @Override - public boolean aggiungi(Object object) { - return false; - } - - // Metodo aggiungi - /* - * Questo metodo aggiunge realmente l'elemento di tipo - * Integer - */ - public boolean aggiungi(Integer integer) { - return super.aggiungi(integer); - } - - // Metodo ordina - /* - * Questo metodo permette il funzionamento dell'ordinamento - * tramite comparatore simulato - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/61/a08bf1565e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/61/a08bf1565e490011162d85db97d6efcd deleted file mode 100644 index e4dfa97..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/61/a08bf1565e490011162d85db97d6efcd +++ /dev/null @@ -1,487 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 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 node) { - if (node == null) return 0; - - int nLeft = (node.hasLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.hasRight() == null) ? 0 : numberLeaf(node.getRight()); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/61/d03e4efa3c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/61/d03e4efa3c4600111c1cd5b6f02d115c deleted file mode 100644 index 4ab30b5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/61/d03e4efa3c4600111c1cd5b6f02d115c +++ /dev/null @@ -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[i]; - vettore[i] = temp; - } - } - - // FUNZIONI ASTRATTE - - /** - * Funzione che restituisce il valore della comparazione - */ - protected abstract int confronta(Object elemento1, Object elemento2); -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/62/109f384477460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/62/109f384477460011162d85db97d6efcd deleted file mode 100644 index a4a31c4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/62/109f384477460011162d85db97d6efcd +++ /dev/null @@ -1,149 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - - while (cur.parent != null){ - d++; - cur = cur.parent; - } - - return d; - } - - public int height() { - if (left == null && right == null) return 0; - - int hLeft = (left == null) ? 0 : left.height(); - int hRight = (right == null) ? 0 : right.height(); - - return Math.max(hLeft, hRight) + 1; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/62/60a5473f3f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/62/60a5473f3f4600111c1cd5b6f02d115c deleted file mode 100644 index 6f877fb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/62/60a5473f3f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,31 +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 - */ - @Override - public boolean aggiungi(Object object) { - return false; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/62/70882dbe5c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/62/70882dbe5c490011162d85db97d6efcd deleted file mode 100644 index a1c332d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/62/70882dbe5c490011162d85db97d6efcd +++ /dev/null @@ -1,465 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - - } - - public void printLeaf(BinaryNode node) { - if (node == null) return; - - if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/62/70d7a0222e470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/62/70d7a0222e470011162d85db97d6efcd deleted file mode 100644 index 7a2255e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/62/70d7a0222e470011162d85db97d6efcd +++ /dev/null @@ -1,113 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - if (temp != null) { - temp.setData(null); - return true; - } - return false; - } - - public boolean contains(E targetElement) { - return find(targetElement, root) != null; - } - - // ITERATORI - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/62/c015439530470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/62/c015439530470011162d85db97d6efcd deleted file mode 100644 index ec342bc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/62/c015439530470011162d85db97d6efcd +++ /dev/null @@ -1,156 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/62/d08425b4314d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/62/d08425b4314d0011135ca7e1c9885acd new file mode 100644 index 0000000..f02d40f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/62/d08425b4314d0011135ca7e1c9885acd @@ -0,0 +1,61 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + head = newNode; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/63/40d1df262f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/63/40d1df262f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..439a6b7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/63/40d1df262f4d0011135ca7e1c9885acd @@ -0,0 +1,34 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node tail = null; + private int size = 0; + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/63/501b8f83374d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/63/501b8f83374d0011135ca7e1c9885acd new file mode 100644 index 0000000..998fb4e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/63/501b8f83374d0011135ca7e1c9885acd @@ -0,0 +1,113 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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(); + if (size == 0) { + head = tail = new Node(item, null, null); + } else { + + } + size++; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/63/806896d0ac4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/63/806896d0ac4c0011135ca7e1c9885acd new file mode 100644 index 0000000..f4dd1fa --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/63/806896d0ac4c0011135ca7e1c9885acd @@ -0,0 +1,43 @@ +package jcf_map.exercise; + +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 > int ContaDuplicati(BinaryNode node) { + int dup = 0; + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + + for (T element : mappa.keySet()) { + int occurrency = mappa.get(element); + if (occurrency > 1) dup++; + } + + return dup; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/63/a04718d47d4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/63/a04718d47d4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..5905550 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/63/a04718d47d4c00111d9cbe34e2b6027a @@ -0,0 +1,19 @@ +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) { + this.nominativo = nominativo; + this.cf = cf; + this.cittaResidenza = cittaResidenza; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/63/c0ab895c6a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/63/c0ab895c6a460011162d85db97d6efcd deleted file mode 100644 index b536a2c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/63/c0ab895c6a460011162d85db97d6efcd +++ /dev/null @@ -1,81 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (this.parent == null) return null; - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/63/d094954042490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/63/d094954042490011162d85db97d6efcd deleted file mode 100644 index fa254aa..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/63/d094954042490011162d85db97d6efcd +++ /dev/null @@ -1,354 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 - * - * - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/64/4061172868460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/64/4061172868460011162d85db97d6efcd deleted file mode 100644 index 4d671dc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/64/4061172868460011162d85db97d6efcd +++ /dev/null @@ -1,56 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode getParent() { - return parent; - } - - public boolean isRoot() { - return parent == null; - } - - public boolean isLeftChild() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/64/4081e65d3c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/64/4081e65d3c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..1e8c137 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/64/4081e65d3c4d0011135ca7e1c9885acd @@ -0,0 +1,168 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for (int i = 0; i < size; i++) { + currentNode = currentNode.next; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/65/40b6f28330470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/65/40b6f28330470011162d85db97d6efcd deleted file mode 100644 index 058058f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/65/40b6f28330470011162d85db97d6efcd +++ /dev/null @@ -1,154 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/65/c08427a130470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/65/c08427a130470011162d85db97d6efcd deleted file mode 100644 index 403d4fd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/65/c08427a130470011162d85db97d6efcd +++ /dev/null @@ -1,159 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/65/f0387a6b41490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/65/f0387a6b41490011162d85db97d6efcd deleted file mode 100644 index 8c0d061..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/65/f0387a6b41490011162d85db97d6efcd +++ /dev/null @@ -1,342 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - itinorder(root, temporaryList); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/66/5030cdf92e4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/66/5030cdf92e4600111c1cd5b6f02d115c deleted file mode 100644 index 4e0fed7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/66/5030cdf92e4600111c1cd5b6f02d115c +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * - */ -module asdl { -} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/66/c03223d29b4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/66/c03223d29b4c0011135ca7e1c9885acd new file mode 100644 index 0000000..4811724 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/66/c03223d29b4c0011135ca7e1c9885acd @@ -0,0 +1,30 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + if (myList.isEmpty()) return true; + // Verifica che la lista si pari o dispari + HashMap valori = new HashMap(); + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/67/305327802c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/67/305327802c470011162d85db97d6efcd deleted file mode 100644 index d0bcfcb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/67/305327802c470011162d85db97d6efcd +++ /dev/null @@ -1,97 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode resNode; - resNode = find(targetElement, root.getLeft()); - if (resNode == null) resNode = find(targetElement, root.getRight()); - return resNode; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/67/7018ca9c9e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/67/7018ca9c9e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..d61b72d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/67/7018ca9c9e4c0011135ca7e1c9885acd @@ -0,0 +1,49 @@ +package jcf_map.exercise; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/67/70d9fc3c7e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/67/70d9fc3c7e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..465399c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/67/70d9fc3c7e4c00111d9cbe34e2b6027a @@ -0,0 +1,44 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw NullPointerException(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/67/e009c3f5a94c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/67/e009c3f5a94c0011135ca7e1c9885acd new file mode 100644 index 0000000..4ad9786 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/67/e009c3f5a94c0011135ca7e1c9885acd @@ -0,0 +1,22 @@ +package jcf_map.exercise; + +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 Map ContaDuplicati(BinaryNode node) { + + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/67/f0533aae67460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/67/f0533aae67460011162d85db97d6efcd deleted file mode 100644 index dcf2552..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/67/f0533aae67460011162d85db97d6efcd +++ /dev/null @@ -1,33 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode right) { - this.data = data; - - this.left = left; - this.right = right; - - this.parent = null; - - if (this.left != null) - } - - // METODI - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/67/f0eadc1981460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/67/f0eadc1981460011162d85db97d6efcd deleted file mode 100644 index 605e846..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/67/f0eadc1981460011162d85db97d6efcd +++ /dev/null @@ -1,38 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(left.root, data, right.root); - size = 1 + left.size + right.size; - } - - // METODI - - public int size() { - return size; - } - - - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/68/607bdda481460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/68/607bdda481460011162d85db97d6efcd deleted file mode 100644 index cecca25..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/68/607bdda481460011162d85db97d6efcd +++ /dev/null @@ -1,41 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/68/80fddd7f42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/68/80fddd7f42490011162d85db97d6efcd deleted file mode 100644 index 69b01f2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/68/80fddd7f42490011162d85db97d6efcd +++ /dev/null @@ -1,360 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/69/905b0ad83c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/69/905b0ad83c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..a1ba6c8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/69/905b0ad83c4d0011135ca7e1c9885acd @@ -0,0 +1,179 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + return currentNode.data; + } + + public E set(int index, E item) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + + Node currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + E oldData = currentNode.data; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/d034aa6e024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/69/a0785a011e4d0011135ca7e1c9885acd similarity index 100% rename from .metadata/.plugins/org.eclipse.core.resources/.history/ac/d034aa6e024c00111d9cbe34e2b6027a rename to .metadata/.plugins/org.eclipse.core.resources/.history/69/a0785a011e4d0011135ca7e1c9885acd diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/69/b034e4bb5c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/69/b034e4bb5c490011162d85db97d6efcd deleted file mode 100644 index cd8cfd2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/69/b034e4bb5c490011162d85db97d6efcd +++ /dev/null @@ -1,465 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - - } - - public void printLeaf(BinaryNode node) { - if (node == null) return; - - if (node.getLeft() == null && node.getRight() == null) System.out.println(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/69/c09f8a6b59490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/69/c09f8a6b59490011162d85db97d6efcd deleted file mode 100644 index 04237c8..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/69/c09f8a6b59490011162d85db97d6efcd +++ /dev/null @@ -1,423 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerExeption(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/69/d0f50675324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/69/d0f50675324d0011135ca7e1c9885acd new file mode 100644 index 0000000..e0d8fc3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/69/d0f50675324d0011135ca7e1c9885acd @@ -0,0 +1,67 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + newNode.next = head; + head = newNode; + } + size++; + } + + public E removeFirst() { + if (head == null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/404291a93b4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/404291a93b4600111c1cd5b6f02d115c deleted file mode 100644 index 6ad52cb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/404291a93b4600111c1cd5b6f02d115c +++ /dev/null @@ -1,62 +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 ordina l'array - */ - public void ordina() { - for (int i = 0; i < dimensioneCorrente; i++) { - - } - } - - // FUNZIONI ASTRATTE - - /** - * Funzione che restituisce il valore della comparazione - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/504b1c6b7f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/504b1c6b7f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..ff7ed86 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/504b1c6b7f4c00111d9cbe34e2b6027a @@ -0,0 +1,55 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + for (String s : archivio.keySet()) { + Cliente currentCliente = archivio.get(s); + String currentCitta = currentCliente.cittaResidenza(); + + if (currentCitta.equals(citta)) autoSelezionate.put(s, currentCliente); + } + + return autoSelezionate; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/6017fcb975460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/6017fcb975460011162d85db97d6efcd deleted file mode 100644 index fd4bcc7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/6017fcb975460011162d85db97d6efcd +++ /dev/null @@ -1,125 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.equals) || hasLeft() != otherType.hasLeft() || harRight() != otherType.hasRight()) return false; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/7077731e7e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/7077731e7e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..78ffbf8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/7077731e7e4c00111d9cbe34e2b6027a @@ -0,0 +1,37 @@ +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) { + 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(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/70aa679e3e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/70aa679e3e490011162d85db97d6efcd deleted file mode 100644 index 5edebd0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/70aa679e3e490011162d85db97d6efcd +++ /dev/null @@ -1,303 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/8006081a69460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/8006081a69460011162d85db97d6efcd deleted file mode 100644 index 9cfcf1e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/8006081a69460011162d85db97d6efcd +++ /dev/null @@ -1,73 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/80e6037f3a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/80e6037f3a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..77759b8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/80e6037f3a4d0011135ca7e1c9885acd @@ -0,0 +1,145 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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; + } + + if (index == size) { + addLast(item); + return; + } + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/90be0379354d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/90be0379354d0011135ca7e1c9885acd new file mode 100644 index 0000000..9dd4186 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/90be0379354d0011135ca7e1c9885acd @@ -0,0 +1,85 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node secondPlace = head.next; + + head = secondPlace; + secondPlace.prev = null; + firstPlace.next = null; + } + size--; + return tmp; + } + + public E getLast() { + if (tail == null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/e0fa43622f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/e0fa43622f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..fc1f2a8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/e0fa43622f4d0011135ca7e1c9885acd @@ -0,0 +1,44 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node tail = null; + private int size = 0; + + // Costruttore + public MyLinkedList() {} + + // Metodi + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/f0b866862f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/f0b866862f470011162d85db97d6efcd deleted file mode 100644 index 552c108..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/f0b866862f470011162d85db97d6efcd +++ /dev/null @@ -1,131 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6b/202041e130470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6b/202041e130470011162d85db97d6efcd deleted file mode 100644 index c8a7436..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6b/202041e130470011162d85db97d6efcd +++ /dev/null @@ -1,166 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - inorder(root, temporaryList); - return temporaryList.iterator(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6b/50a69a65374d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/6b/50a69a65374d0011135ca7e1c9885acd new file mode 100644 index 0000000..c267135 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6b/50a69a65374d0011135ca7e1c9885acd @@ -0,0 +1,108 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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(); + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6b/f044e4ff4f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6b/f044e4ff4f490011162d85db97d6efcd deleted file mode 100644 index 1deb801..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6b/f044e4ff4f490011162d85db97d6efcd +++ /dev/null @@ -1,407 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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. - */ - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6c/6037ed4c084c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/6c/6037ed4c084c00111d9cbe34e2b6027a deleted file mode 100644 index d7ed3a7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6c/6037ed4c084c00111d9cbe34e2b6027a +++ /dev/null @@ -1,9 +0,0 @@ -package jcf_set.exercise; - -public class Insiemistica { - - public static void main(String[] main) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6c/e08a4fe085460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6c/e08a4fe085460011162d85db97d6efcd deleted file mode 100644 index f571c20..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6c/e08a4fe085460011162d85db97d6efcd +++ /dev/null @@ -1,79 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6d/204cb9d82f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/6d/204cb9d82f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..aff4eee --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6d/204cb9d82f4d0011135ca7e1c9885acd @@ -0,0 +1,55 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6d/40a0f22eaa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/6d/40a0f22eaa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..febf3d8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6d/40a0f22eaa4c0011135ca7e1c9885acd @@ -0,0 +1,25 @@ +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + if (node == nukk) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6d/609f95e37e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/6d/609f95e37e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..525a920 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6d/609f95e37e4c00111d9cbe34e2b6027a @@ -0,0 +1,50 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + for (String s : archivio.keySet()) { + Cliente currentCliente = archivio.get(s); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6d/b02166c3324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/6d/b02166c3324d0011135ca7e1c9885acd new file mode 100644 index 0000000..6d0a182 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6d/b02166c3324d0011135ca7e1c9885acd @@ -0,0 +1,74 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + newNode.next = head; + head = newNode; + } + size++; + } + + public E removeFirst() { + if (head == null) throw new NoSuchElementException(); + E tmp; + if (size == 1) { + tmp = head.data; + head = tail = null; + return tmp; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6e/40d91aaa6a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6e/40d91aaa6a460011162d85db97d6efcd deleted file mode 100644 index 3c88f8c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6e/40d91aaa6a460011162d85db97d6efcd +++ /dev/null @@ -1,90 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6e/d0764a0c3f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6e/d0764a0c3f490011162d85db97d6efcd deleted file mode 100644 index fe5b568..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6e/d0764a0c3f490011162d85db97d6efcd +++ /dev/null @@ -1,308 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - if (current.getRight() != null) queueOfNodes.push(current.getRight()); - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6e/f002016436470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6e/f002016436470011162d85db97d6efcd deleted file mode 100644 index fab6862..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6e/f002016436470011162d85db97d6efcd +++ /dev/null @@ -1,232 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6f/50ff01ac3a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/6f/50ff01ac3a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..f7138a6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6f/50ff01ac3a4d0011135ca7e1c9885acd @@ -0,0 +1,145 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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; + } + + Node newNode = new Node(item, null, null); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7/80076b54324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/7/80076b54324d0011135ca7e1c9885acd new file mode 100644 index 0000000..3488c82 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7/80076b54324d0011135ca7e1c9885acd @@ -0,0 +1,67 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + newNode.next = head; + head = newNode; + } + size++; + } + + public E removeFirst() { + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/70/50a1d083ac4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/70/50a1d083ac4c0011135ca7e1c9885acd new file mode 100644 index 0000000..b4a43f4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/70/50a1d083ac4c0011135ca7e1c9885acd @@ -0,0 +1,42 @@ +package jcf_map.exercise; + +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 int ContaDuplicati(BinaryNode node) { + int dup = 0; + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + + for (T element : mappa.keySet()) { + + } + + return dup; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/70/60e681c43c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/70/60e681c43c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..4e75637 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/70/60e681c43c4d0011135ca7e1c9885acd @@ -0,0 +1,176 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + return currentNode.data; + } + + public E set(int index, E item) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + + Node currentNode = head; + E oldData; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/70/80c01fca354d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/70/80c01fca354d0011135ca7e1c9885acd new file mode 100644 index 0000000..1ec2256 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/70/80c01fca354d0011135ca7e1c9885acd @@ -0,0 +1,91 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/70/c034a8002d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/70/c034a8002d470011162d85db97d6efcd deleted file mode 100644 index 9e6be2c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/70/c034a8002d470011162d85db97d6efcd +++ /dev/null @@ -1,103 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - if (temp != null) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/72/3028dd53aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/72/3028dd53aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..18ae88d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/72/3028dd53aa4c0011135ca7e1c9885acd @@ -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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + if (node == null) return mappa; + + if (node.getLeft() != null) ContaDuplicati(node.getLeft(), mappa); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/72/30bc84b83f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/72/30bc84b83f490011162d85db97d6efcd deleted file mode 100644 index 2c91499..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/72/30bc84b83f490011162d85db97d6efcd +++ /dev/null @@ -1,311 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - // Aggiungiamo il nodo di destra - if (current.getRight() != null) queueOfNodes.push(current.getRight()); - // Aggiungiamo - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/72/30e8bfad35470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/72/30e8bfad35470011162d85db97d6efcd deleted file mode 100644 index de704f0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/72/30e8bfad35470011162d85db97d6efcd +++ /dev/null @@ -1,226 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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()); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/72/602cb7cf67460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/72/602cb7cf67460011162d85db97d6efcd deleted file mode 100644 index eea068f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/72/602cb7cf67460011162d85db97d6efcd +++ /dev/null @@ -1,36 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/74/304022b735470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/74/304022b735470011162d85db97d6efcd deleted file mode 100644 index d3c5815..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/74/304022b735470011162d85db97d6efcd +++ /dev/null @@ -1,227 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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) - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/74/4011737859490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/74/4011737859490011162d85db97d6efcd deleted file mode 100644 index 8f98dd2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/74/4011737859490011162d85db97d6efcd +++ /dev/null @@ -1,423 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/74/703091473c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/74/703091473c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..c154c68 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/74/703091473c4d0011135ca7e1c9885acd @@ -0,0 +1,165 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/74/902a2127304d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/74/902a2127304d0011135ca7e1c9885acd new file mode 100644 index 0000000..4c6a117 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/74/902a2127304d0011135ca7e1c9885acd @@ -0,0 +1,59 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(); + } else { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/74/b06ff4147e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/74/b06ff4147e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..3080e9f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/74/b06ff4147e4c00111d9cbe34e2b6027a @@ -0,0 +1,35 @@ +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) { + 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 + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/74/d04f9934aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/74/d04f9934aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..a10e997 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/74/d04f9934aa4c0011135ca7e1c9885acd @@ -0,0 +1,25 @@ +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + if (node == null) return mappa; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/75/308fa1528f4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/75/308fa1528f4c0011135ca7e1c9885acd new file mode 100644 index 0000000..97ed41d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/75/308fa1528f4c0011135ca7e1c9885acd @@ -0,0 +1,20 @@ +package jcf_map.exercise; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/75/405922ed3d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/75/405922ed3d490011162d85db97d6efcd deleted file mode 100644 index 4d7c175..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/75/405922ed3d490011162d85db97d6efcd +++ /dev/null @@ -1,282 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - itpreorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATOR - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/75/60b78aff7d4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/75/60b78aff7d4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..fee04d1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/75/60b78aff7d4c00111d9cbe34e2b6027a @@ -0,0 +1,35 @@ +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) { + 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; + } + + // Equals + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/75/70103e427e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/75/70103e427e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..1cb8099 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/75/70103e427e4c00111d9cbe34e2b6027a @@ -0,0 +1,44 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/75/b056aedb80460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/75/b056aedb80460011162d85db97d6efcd deleted file mode 100644 index 4e200b9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/75/b056aedb80460011162d85db97d6efcd +++ /dev/null @@ -1,31 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(left.root, data, right.root); - } - - // METODI - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/76/20550e531b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/76/20550e531b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..3546e9c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/76/20550e531b4d0011135ca7e1c9885acd @@ -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 mappaAssociazione = new TreeMap(); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/76/304f2e47a84c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/76/304f2e47a84c0011135ca7e1c9885acd new file mode 100644 index 0000000..372bb1e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/76/304f2e47a84c0011135ca7e1c9885acd @@ -0,0 +1,7 @@ +package jcf_map.exercise; + +public class BinaryTreeMap { + /* + * + */ +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/76/90a15fc1a94c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/76/90a15fc1a94c0011135ca7e1c9885acd new file mode 100644 index 0000000..6f72c9e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/76/90a15fc1a94c0011135ca7e1c9885acd @@ -0,0 +1,14 @@ +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. + * 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 Map + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/76/d0c25c7e024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/76/d0c25c7e024c00111d9cbe34e2b6027a deleted file mode 100644 index aea4298..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/76/d0c25c7e024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,7 +0,0 @@ -package jcf_set.exercise; - -public class IteratoreSenzaDuplicati { - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/76/f05ffd725d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/76/f05ffd725d490011162d85db97d6efcd deleted file mode 100644 index 1c62be9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/76/f05ffd725d490011162d85db97d6efcd +++ /dev/null @@ -1,482 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public void numberLeaf() { - Integer number = 0; - numberLeaf(root, number); - } - - public void numberLeaf(BinaryNode node, Integer number) { - if (node == null) return; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/30ac586d5d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/77/30ac586d5d490011162d85db97d6efcd deleted file mode 100644 index 53063a9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/77/30ac586d5d490011162d85db97d6efcd +++ /dev/null @@ -1,482 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public void numberLeaf() { - Integer number = 0; - numberLeaf(root, number); - } - - public void numberLeaf(BinaryNode node, Integer number) { - if () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/8031d2d73f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/77/8031d2d73f490011162d85db97d6efcd deleted file mode 100644 index 5894709..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/77/8031d2d73f490011162d85db97d6efcd +++ /dev/null @@ -1,314 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - Boolean flag; - - queueOfNodes.add(node); - flags.push(false); - - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - flag = flags.pop(); - // Aggiungiamo il nodo di destra - if (current.getRight() != null) queueOfNodes.push(current.getRight()); - // Aggiungiamo - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/809a4c57ac4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/77/809a4c57ac4c0011135ca7e1c9885acd new file mode 100644 index 0000000..95a5eb0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/77/809a4c57ac4c0011135ca7e1c9885acd @@ -0,0 +1,38 @@ +package jcf_map.exercise; + +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 int ContaDuplicati(BinaryNode node) { + int dup = 0; + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + + return dup; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/a08d42473e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/77/a08d42473e490011162d85db97d6efcd deleted file mode 100644 index 614a2a5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/77/a08d42473e490011162d85db97d6efcd +++ /dev/null @@ -1,291 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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. - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/b06e0bee384d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/77/b06e0bee384d0011135ca7e1c9885acd new file mode 100644 index 0000000..6701b51 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/77/b06e0bee384d0011135ca7e1c9885acd @@ -0,0 +1,126 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node oldLastNode = tail; + oldLastNode.next = newNode; + newNode.prev = oldLastNode; + tail = newNode; + } + size++; + } + + public void clear() { + head = tail = null; + size = 0; + } + + public boolean add(E item) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/b07a2b7767460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/77/b07a2b7767460011162d85db97d6efcd deleted file mode 100644 index 5b85e8d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/77/b07a2b7767460011162d85db97d6efcd +++ /dev/null @@ -1,27 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode right) { - this.data = data; - this.left = left; - this.right = right; - } - - // METODI - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/d09dfc6776460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/77/d09dfc6776460011162d85db97d6efcd deleted file mode 100644 index e04fb83..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/77/d09dfc6776460011162d85db97d6efcd +++ /dev/null @@ -1,137 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - - while (cur.parent != null){ - d++; - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/78/10c52150324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/78/10c52150324d0011135ca7e1c9885acd new file mode 100644 index 0000000..f76b203 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/78/10c52150324d0011135ca7e1c9885acd @@ -0,0 +1,63 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + newNode.next = head; + head = newNode; + } + size++; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/78/901da8f7a94c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/78/901da8f7a94c0011135ca7e1c9885acd new file mode 100644 index 0000000..d97c883 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/78/901da8f7a94c0011135ca7e1c9885acd @@ -0,0 +1,22 @@ +package jcf_map.exercise; + +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/78/b09b9737aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/78/b09b9737aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..ede32f5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/78/b09b9737aa4c0011135ca7e1c9885acd @@ -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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + if (node == null) return mappa; + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/79/009c09eb1a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/79/009c09eb1a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..69d14ef --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/79/009c09eb1a4d0011135ca7e1c9885acd @@ -0,0 +1,13 @@ +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. + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/79/400d4ff333490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/79/400d4ff333490011162d85db97d6efcd deleted file mode 100644 index eaf53b0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/79/400d4ff333490011162d85db97d6efcd +++ /dev/null @@ -1,271 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 il primo 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - if (current.getData() != null) temporaryList.add(current.getData()); - if () - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/79/406738f531470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/79/406738f531470011162d85db97d6efcd deleted file mode 100644 index 40f0a7a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/79/406738f531470011162d85db97d6efcd +++ /dev/null @@ -1,190 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - postorder(root, temporaryList); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/79/604fa8e3024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/79/604fa8e3024c00111d9cbe34e2b6027a deleted file mode 100644 index 8b78843..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/79/604fa8e3024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,25 +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 lista = new ArrayList(); - - lista.add("tree"); - lista.add("flower"); - lista.add("tree"); - lista.add("flower"); - lista.add("animal"); - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/79/8059808d6b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/79/8059808d6b460011162d85db97d6efcd deleted file mode 100644 index 15376c9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/79/8059808d6b460011162d85db97d6efcd +++ /dev/null @@ -1,110 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode oldParent = parent; - parent = null; - - if (oldParent.left == this) oldParent.left = null; - else oldParent.right = null; - - return oldParent; - } - - public boolean hasLeft() { - return left != null; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/79/80f169c39e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/79/80f169c39e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..bbf2b27 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/79/80f169c39e4c0011135ca7e1c9885acd @@ -0,0 +1,49 @@ +package jcf_map.exercise; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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. + myList.sort(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/79/c0797d61334d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/79/c0797d61334d0011135ca7e1c9885acd new file mode 100644 index 0000000..2db59ff --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/79/c0797d61334d0011135ca7e1c9885acd @@ -0,0 +1,77 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 { + head.next.prev = null; + head.next = null; + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/79/d04bc058094c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/79/d04bc058094c00111d9cbe34e2b6027a deleted file mode 100644 index f2aac67..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/79/d04bc058094c00111d9cbe34e2b6027a +++ /dev/null @@ -1,32 +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 aHashSet = new HashSet(Arrays.asList(aArray)); - Set bHashSet = new HashSet(Arrays.asList(bArray)); - - - - } - - // Operazioni di insiemistica - - /* - * Per eseguire una unione, è conveniente - * adoperare la Set poichè l'implementazione - * TreeSet permette di non mantenere duplicati. - */ - public static > - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/79/d0de88a65d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/79/d0de88a65d490011162d85db97d6efcd deleted file mode 100644 index bf4695b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/79/d0de88a65d490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public int numberLeaf() { - Integer number = 0; - numberLeaf(root, number); - } - - public void numberLeaf(BinaryNode node, Integer number) { - if (node == null) return; - - if (node.getLeft() == null && node.getRight() == null) number++; - else { - numberLeaf(node.getLeft(), number); - numberLeaf(node.getRight(), number); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/79/e001ee783e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/79/e001ee783e490011162d85db97d6efcd deleted file mode 100644 index 897c3c4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/79/e001ee783e490011162d85db97d6efcd +++ /dev/null @@ -1,299 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7a/a0edc6ee31470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7a/a0edc6ee31470011162d85db97d6efcd deleted file mode 100644 index 3dfcbcd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7a/a0edc6ee31470011162d85db97d6efcd +++ /dev/null @@ -1,190 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - postorder(root, ); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7a/e0c9c2fe354d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/7a/e0c9c2fe354d0011135ca7e1c9885acd new file mode 100644 index 0000000..454116e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7a/e0c9c2fe354d0011135ca7e1c9885acd @@ -0,0 +1,92 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7b/106f4457aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/7b/106f4457aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..059c970 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7b/106f4457aa4c0011135ca7e1c9885acd @@ -0,0 +1,28 @@ +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + if (node == null) return mappa; + + if (node.getLeft() != null) ContaDuplicati(node.getLeft(), mappa); + if (node.getRight() != null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7b/a044075a80460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7b/a044075a80460011162d85db97d6efcd deleted file mode 100644 index bd4e8db..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7b/a044075a80460011162d85db97d6efcd +++ /dev/null @@ -1,26 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - } - - // METODI - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7b/c01006151f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/7b/c01006151f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..b91630b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7b/c01006151f4d0011135ca7e1c9885acd @@ -0,0 +1,32 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node tail = null; + private int size = 0; + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7c/60fa04d99b4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/7c/60fa04d99b4c0011135ca7e1c9885acd new file mode 100644 index 0000000..a8436fa --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7c/60fa04d99b4c0011135ca7e1c9885acd @@ -0,0 +1,31 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + if (myList.isEmpty()) return true; + // Verifica che la lista si pari o dispari + HashMap valori = new HashMap(); + + Iterator it = myList.iterator(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7c/d002ffc682460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7c/d002ffc682460011162d85db97d6efcd deleted file mode 100644 index a3d0ccf..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7c/d002ffc682460011162d85db97d6efcd +++ /dev/null @@ -1,58 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode node) { - if (node.getLeft() == null && node.getRight() == null) return 0; - - int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7d/10d3ddbe354d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/7d/10d3ddbe354d0011135ca7e1c9885acd new file mode 100644 index 0000000..b46308c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7d/10d3ddbe354d0011135ca7e1c9885acd @@ -0,0 +1,90 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7d/602356d640490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7d/602356d640490011162d85db97d6efcd deleted file mode 100644 index c4a2ae7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7d/602356d640490011162d85db97d6efcd +++ /dev/null @@ -1,328 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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); - flag.push(); - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7d/d0187f7a33470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7d/d0187f7a33470011162d85db97d6efcd deleted file mode 100644 index d683f58..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7d/d0187f7a33470011162d85db97d6efcd +++ /dev/null @@ -1,212 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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) - */ - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7e/108e7c3e9e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/7e/108e7c3e9e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..576dc47 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7e/108e7c3e9e4c0011135ca7e1c9885acd @@ -0,0 +1,42 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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à + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7e/2030127d3e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7e/2030127d3e490011162d85db97d6efcd deleted file mode 100644 index 71d924c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7e/2030127d3e490011162d85db97d6efcd +++ /dev/null @@ -1,299 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>();) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7e/a093cda230470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7e/a093cda230470011162d85db97d6efcd deleted file mode 100644 index a8a333c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7e/a093cda230470011162d85db97d6efcd +++ /dev/null @@ -1,161 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7e/b057d02285460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7e/b057d02285460011162d85db97d6efcd deleted file mode 100644 index 6b655af..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7e/b057d02285460011162d85db97d6efcd +++ /dev/null @@ -1,69 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - return leftTree; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7e/b09cfb569e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/7e/b09cfb569e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..4a503a1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7e/b09cfb569e4c0011135ca7e1c9885acd @@ -0,0 +1,44 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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); + } + for (E chiave : valori.keySet()) { + int valore = valori.get(chiave); + } + // Controllo short circuit di disparità + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/4031c61b69460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/4031c61b69460011162d85db97d6efcd deleted file mode 100644 index bd4d256..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/4031c61b69460011162d85db97d6efcd +++ /dev/null @@ -1,73 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/6005a01d7f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/6005a01d7f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..58a68d9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/6005a01d7f4c00111d9cbe34e2b6027a @@ -0,0 +1,52 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + for (String s : archivio.keySet()) { + Cliente currentCliente = archivio.get(s); + String currentCitta = currentCliente.cittaResidenza(); + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/601ec91b51490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/601ec91b51490011162d85db97d6efcd deleted file mode 100644 index 8c4d4aa..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/601ec91b51490011162d85db97d6efcd +++ /dev/null @@ -1,414 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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: - * - */ - public LinkedBinaryTree(List objectList) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/80811bb576460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/80811bb576460011162d85db97d6efcd deleted file mode 100644 index a236240..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/80811bb576460011162d85db97d6efcd +++ /dev/null @@ -1,144 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - - while (cur.parent != null){ - d++; - cur = cur.parent; - } - - return d; - } - - public int height() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/c02658a840490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/c02658a840490011162d85db97d6efcd deleted file mode 100644 index 88f4208..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/c02658a840490011162d85db97d6efcd +++ /dev/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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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(true); - } - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/c069c989024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/c069c989024c00111d9cbe34e2b6027a deleted file mode 100644 index 9b892cb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/c069c989024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,13 +0,0 @@ -package jcf_set.exercise; - -public class IteratoreSenzaDuplicati { - - public static void main(String[] main) { - - } - - public void run() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/d00d2c7f2d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/d00d2c7f2d470011162d85db97d6efcd deleted file mode 100644 index ed40103..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/d00d2c7f2d470011162d85db97d6efcd +++ /dev/null @@ -1,111 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - if (temp != null) { - temp.setData(null); - return true; - } - return false; - } - - public boolean contains() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8/2044863a5d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8/2044863a5d490011162d85db97d6efcd deleted file mode 100644 index 83a4933..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8/2044863a5d490011162d85db97d6efcd +++ /dev/null @@ -1,477 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public void numberLeaf() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8/8021abdda94c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/8/8021abdda94c0011135ca7e1c9885acd new file mode 100644 index 0000000..236ab7b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8/8021abdda94c0011135ca7e1c9885acd @@ -0,0 +1,18 @@ +package jcf_map.exercise; + +import java.util.Map; + +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 Map ContaDuplicati() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8/8034c7a08f4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/8/8034c7a08f4c0011135ca7e1c9885acd new file mode 100644 index 0000000..c6a316c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8/8034c7a08f4c0011135ca7e1c9885acd @@ -0,0 +1,24 @@ +package jcf_map.exercise; + +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/80/10dc9282aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/80/10dc9282aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..180b5ee --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/80/10dc9282aa4c0011135ca7e1c9885acd @@ -0,0 +1,31 @@ +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap 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; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/80/40df10af354d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/80/40df10af354d0011135ca7e1c9885acd new file mode 100644 index 0000000..f023c98 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/80/40df10af354d0011135ca7e1c9885acd @@ -0,0 +1,88 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/80/b0078850314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/80/b0078850314600111c1cd5b6f02d115c deleted file mode 100644 index ce4ff1b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/80/b0078850314600111c1cd5b6f02d115c +++ /dev/null @@ -1,18 +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; - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/80/c040020767460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/80/c040020767460011162d85db97d6efcd deleted file mode 100644 index 733a2ab..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/80/c040020767460011162d85db97d6efcd +++ /dev/null @@ -1,14 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // METODI - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/81/208f964238470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/81/208f964238470011162d85db97d6efcd deleted file mode 100644 index d5cb216..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/81/208f964238470011162d85db97d6efcd +++ /dev/null @@ -1,239 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator iterator() { - return iteratorPreOrder(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/81/5038b84141490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/81/5038b84141490011162d85db97d6efcd deleted file mode 100644 index edd12d6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/81/5038b84141490011162d85db97d6efcd +++ /dev/null @@ -1,336 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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); - } - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/81/9093a27430470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/81/9093a27430470011162d85db97d6efcd deleted file mode 100644 index b2677cb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/81/9093a27430470011162d85db97d6efcd +++ /dev/null @@ -1,154 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/81/c0e49baf304600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/81/c0e49baf304600111c1cd5b6f02d115c deleted file mode 100644 index fc5f690..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/81/c0e49baf304600111c1cd5b6f02d115c +++ /dev/null @@ -1,9 +0,0 @@ -package vettore_ordinabile; - -public abstract class VettoreOrdinabile { - - // Variabili di istanza - private Object[] vettore; // Vettore dove memorizzare i dati - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/82/20365eeb3f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/82/20365eeb3f4600111c1cd5b6f02d115c deleted file mode 100644 index 13d3f37..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/82/20365eeb3f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,55 +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 - */ - @Override - public boolean aggiungi(Object object) { - return false; - } - - // Metodo aggiungi - /* - * Questo metodo aggiunge realmente l'elemento di tipo - * Integer - */ - public boolean aggiungi(Integer integer) { - return super.aggiungi(integer); - } - - // Metodo ordina - /* - * Questo metodo permette il funzionamento dell'ordinamento - * tramite comparatore simulato - */ - @Override - public int ordina(Object elemento1, Object elemento2) { - Integer i1 = (Integer)elemento1; - Integer i2 = (Integer)elemento2; - - if (i1 < i2) return -1; - if (i1 > i2) return 1; - return 0; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/82/30a092198f4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/82/30a092198f4c0011135ca7e1c9885acd new file mode 100644 index 0000000..e64547d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/82/30a092198f4c0011135ca7e1c9885acd @@ -0,0 +1,11 @@ +package jcf_map.exercise; + +public class Dispari { + + /* + * Esercizio 1 - Esame del 5 settembre 2017 + * + * + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/82/30d8d284324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/82/30d8d284324d0011135ca7e1c9885acd new file mode 100644 index 0000000..45c9aeb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/82/30d8d284324d0011135ca7e1c9885acd @@ -0,0 +1,69 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + newNode.next = head; + head = newNode; + } + size++; + } + + public E removeFirst() { + if (head == null) throw new NoSuchElementException(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/82/4091cea7aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/82/4091cea7aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..bcc4822 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/82/4091cea7aa4c0011135ca7e1c9885acd @@ -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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, TreeMap 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 () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/82/40c592559b4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/82/40c592559b4c0011135ca7e1c9885acd new file mode 100644 index 0000000..cdbd405 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/82/40c592559b4c0011135ca7e1c9885acd @@ -0,0 +1,27 @@ +package jcf_map.exercise; + +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + + // Verifica che la lista si pari o dispari + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/82/40c7d74c8f4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/82/40c7d74c8f4c0011135ca7e1c9885acd new file mode 100644 index 0000000..b8b3d55 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/82/40c7d74c8f4c0011135ca7e1c9885acd @@ -0,0 +1,20 @@ +package jcf_map.exercise; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/82/c05c6889ac4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/82/c05c6889ac4c0011135ca7e1c9885acd new file mode 100644 index 0000000..d5f9245 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/82/c05c6889ac4c0011135ca7e1c9885acd @@ -0,0 +1,42 @@ +package jcf_map.exercise; + +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 int ContaDuplicati(BinaryNode node) { + int dup = 0; + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + + for (T element : mappa.keySet()) { + int occurrency = mappa.get(element); + } + + return dup; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/82/c0cc9810394d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/82/c0cc9810394d0011135ca7e1c9885acd new file mode 100644 index 0000000..1b8c7d6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/82/c0cc9810394d0011135ca7e1c9885acd @@ -0,0 +1,130 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/82/f080e74168460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/82/f080e74168460011162d85db97d6efcd deleted file mode 100644 index 8795bb3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/82/f080e74168460011162d85db97d6efcd +++ /dev/null @@ -1,56 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode getParent() { - return parent; - } - - public boolean isRoot() { - return parent == null; - } - - public boolean isLeftChild() { - if () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/0084cfd8024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/83/0084cfd8024c00111d9cbe34e2b6027a deleted file mode 100644 index 75f2aff..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/83/0084cfd8024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,24 +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 lista = new ArrayList(); - - lista.add("tree"); - lista.add("flower"); - lista.add("tree"); - lista.add("flower"); - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/1003f3343d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/83/1003f3343d490011162d85db97d6efcd deleted file mode 100644 index c487f8b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/83/1003f3343d490011162d85db97d6efcd +++ /dev/null @@ -1,274 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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()); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/301f3bfb344d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/83/301f3bfb344d0011135ca7e1c9885acd new file mode 100644 index 0000000..edbc633 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/83/301f3bfb344d0011135ca7e1c9885acd @@ -0,0 +1,82 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node secondPlace = head.next; + + head = secondPlace; + secondPlace.prev = null; + firstPlace.next = null; + + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/30b24fb66a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/83/30b24fb66a460011162d85db97d6efcd deleted file mode 100644 index e09d78d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/83/30b24fb66a460011162d85db97d6efcd +++ /dev/null @@ -1,92 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/80f9dad840490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/83/80f9dad840490011162d85db97d6efcd deleted file mode 100644 index 3c1c09f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/83/80f9dad840490011162d85db97d6efcd +++ /dev/null @@ -1,328 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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); - flag.push(true); - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/906cec683b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/83/906cec683b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..bcd3224 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/83/906cec683b4d0011135ca7e1c9885acd @@ -0,0 +1,153 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node nextNode = prevNode.next; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/e065966f3c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/83/e065966f3c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..bbee58d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/83/e065966f3c4d0011135ca7e1c9885acd @@ -0,0 +1,169 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for (int i = 0; i < size; i++) { + currentNode = currentNode.next; + } + return currentNode.data; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/84/00f6fe732f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/84/00f6fe732f470011162d85db97d6efcd deleted file mode 100644 index 9d5220b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/84/00f6fe732f470011162d85db97d6efcd +++ /dev/null @@ -1,130 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/84/10240f5f5d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/84/10240f5f5d490011162d85db97d6efcd deleted file mode 100644 index 7b1b391..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/84/10240f5f5d490011162d85db97d6efcd +++ /dev/null @@ -1,482 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public void numberLeaf() { - Integer number = 0; - numberLeaf(root); - } - - public void numberLeaf(BinaryNode node) { - if () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/84/203966531b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/84/203966531b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..d4abab2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/84/203966531b4d0011135ca7e1c9885acd @@ -0,0 +1,25 @@ +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 mappaAssociazione = new TreeMap(); + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/84/4014179b7d4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/84/4014179b7d4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..e6800c0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/84/4014179b7d4c00111d9cbe34e2b6027a @@ -0,0 +1,44 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/84/401ed55643490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/84/401ed55643490011162d85db97d6efcd deleted file mode 100644 index b438fcb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/84/401ed55643490011162d85db97d6efcd +++ /dev/null @@ -1,390 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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()); - flags.push(false); - } - - // visitiamo il nodo di sinistra infine - if (current.getLeft() != null) { - queueOfNodes.push(current.getLeft()); - flags.push(false); - } - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/84/40fa2178aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/84/40fa2178aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..cd4d3ac --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/84/40fa2178aa4c0011135ca7e1c9885acd @@ -0,0 +1,30 @@ +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap 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) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/84/801541cd084c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/84/801541cd084c00111d9cbe34e2b6027a deleted file mode 100644 index 8b8ae51..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/84/801541cd084c00111d9cbe34e2b6027a +++ /dev/null @@ -1,20 +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 aHashSet = new HashSet(Arrays.asList(aArray)); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/85/407fb32faa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/85/407fb32faa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..f97efab --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/85/407fb32faa4c0011135ca7e1c9885acd @@ -0,0 +1,25 @@ +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + if (node == null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/85/703fd7f265460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/85/703fd7f265460011162d85db97d6efcd deleted file mode 100644 index b0cf2dd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/85/703fd7f265460011162d85db97d6efcd +++ /dev/null @@ -1,10 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/85/80d69c7930470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/85/80d69c7930470011162d85db97d6efcd deleted file mode 100644 index 54e6973..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/85/80d69c7930470011162d85db97d6efcd +++ /dev/null @@ -1,154 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/85/904d3a6659490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/85/904d3a6659490011162d85db97d6efcd deleted file mode 100644 index ffdeb86..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/85/904d3a6659490011162d85db97d6efcd +++ /dev/null @@ -1,423 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/85/f0fff009804c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/85/f0fff009804c00111d9cbe34e2b6027a new file mode 100644 index 0000000..d6e227a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/85/f0fff009804c00111d9cbe34e2b6027a @@ -0,0 +1,68 @@ +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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(new Comparator() { + @Override + public int compare(String s1, String s2) { + return s1.compareTo(s2); + } + }); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/86/20a4c6bf7f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/86/20a4c6bf7f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..e273557 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/86/20a4c6bf7f4c00111d9cbe34e2b6027a @@ -0,0 +1,63 @@ +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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/86/20d7ee5a41490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/86/20d7ee5a41490011162d85db97d6efcd deleted file mode 100644 index d8dcac9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/86/20d7ee5a41490011162d85db97d6efcd +++ /dev/null @@ -1,340 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/86/300c5deeab4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/86/300c5deeab4c0011135ca7e1c9885acd new file mode 100644 index 0000000..8e7fbbc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/86/300c5deeab4c0011135ca7e1c9885acd @@ -0,0 +1,38 @@ +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 Map ContaDuplicati(BinaryNode node) { + TreeMap mappa = new TreeMap(); + ContaDuplicati(); + } + + protected static Map ContaDuplicati(BinaryNode node, TreeMap 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(data, 1); + else mappa.put(data, mappa.get(data) + 1); + + return mappa; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/86/401789bc7d4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/86/401789bc7d4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..c593437 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/86/401789bc7d4c00111d9cbe34e2b6027a @@ -0,0 +1,17 @@ +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) { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/86/50917a4a84460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/86/50917a4a84460011162d85db97d6efcd deleted file mode 100644 index d1f6390..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/86/50917a4a84460011162d85db97d6efcd +++ /dev/null @@ -1,60 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/86/602066513c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/86/602066513c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..ffc1fdf --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/86/602066513c4d0011135ca7e1c9885acd @@ -0,0 +1,168 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for () { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/86/90b9fc6f2d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/86/90b9fc6f2d470011162d85db97d6efcd deleted file mode 100644 index e90522d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/86/90b9fc6f2d470011162d85db97d6efcd +++ /dev/null @@ -1,107 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - if (temp != null) { - temp.setData(null); - return true; - } - return false; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/86/c04515591e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/86/c04515591e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..011eb11 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/86/c04515591e4d0011135ca7e1c9885acd @@ -0,0 +1,19 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Data + private E data; + + // Riferimenti elemento precedente e successivo + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/86/c08d5f4281460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/86/c08d5f4281460011162d85db97d6efcd deleted file mode 100644 index 2dd43c6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/86/c08d5f4281460011162d85db97d6efcd +++ /dev/null @@ -1,40 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(left.root, data, right.root); - size = 1 + left.size + right.size; - } - - // METODI - - public int size() { - return size; - } - - public boolean isEmpty() { - return root == null; - } - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/87/20b69d351b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/87/20b69d351b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..a4a6f5a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/87/20b69d351b4d0011135ca7e1c9885acd @@ -0,0 +1,21 @@ +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: si intende adoperare chiaramente un TreeMap. + * In modo che vi sia una associazione chiave-valore e un ordinamento basato + * sulla chiave. + * + */ + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/87/504f06ad3c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/87/504f06ad3c4600111c1cd5b6f02d115c deleted file mode 100644 index 01bc290..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/87/504f06ad3c4600111c1cd5b6f02d115c +++ /dev/null @@ -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; - } - int 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); -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/87/602625d530470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/87/602625d530470011162d85db97d6efcd deleted file mode 100644 index 9f5e400..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/87/602625d530470011162d85db97d6efcd +++ /dev/null @@ -1,164 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/87/904a8d643d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/87/904a8d643d490011162d85db97d6efcd deleted file mode 100644 index 4c0e00d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/87/904a8d643d490011162d85db97d6efcd +++ /dev/null @@ -1,280 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 iteratorInOrder() { - - } - - // ITERATOR - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/88/5045f61f38470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/88/5045f61f38470011162d85db97d6efcd deleted file mode 100644 index b9b586f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/88/5045f61f38470011162d85db97d6efcd +++ /dev/null @@ -1,239 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator iterator(){ - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/88/c05cc1267f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/88/c05cc1267f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..33a5614 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/88/c05cc1267f4c00111d9cbe34e2b6027a @@ -0,0 +1,53 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + for (String s : archivio.keySet()) { + Cliente currentCliente = archivio.get(s); + String currentCitta = currentCliente.cittaResidenza(); + + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/89/2005a5003b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/89/2005a5003b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..bc08294 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/89/2005a5003b4d0011135ca7e1c9885acd @@ -0,0 +1,150 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < size; i++) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/89/508509b03e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/89/508509b03e490011162d85db97d6efcd deleted file mode 100644 index 7251773..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/89/508509b03e490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/89/606c3bcfac4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/89/606c3bcfac4c0011135ca7e1c9885acd new file mode 100644 index 0000000..55093bc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/89/606c3bcfac4c0011135ca7e1c9885acd @@ -0,0 +1,43 @@ +package jcf_map.exercise; + +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 int ContaDuplicati(BinaryNode node) { + int dup = 0; + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + + for (T element : mappa.keySet()) { + int occurrency = mappa.get(element); + if (occurrency > 1) dup++; + } + + return dup; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/89/7039b5ae2f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/89/7039b5ae2f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..1864f09 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/89/7039b5ae2f4d0011135ca7e1c9885acd @@ -0,0 +1,54 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/89/a08a8742804c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/89/a08a8742804c00111d9cbe34e2b6027a new file mode 100644 index 0000000..7074e42 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/89/a08a8742804c00111d9cbe34e2b6027a @@ -0,0 +1,64 @@ +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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(archivio); + return archivioOrdinato; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/89/b0e3e9768f4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/89/b0e3e9768f4c0011135ca7e1c9885acd new file mode 100644 index 0000000..c6a316c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/89/b0e3e9768f4c0011135ca7e1c9885acd @@ -0,0 +1,24 @@ +package jcf_map.exercise; + +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8a/309bb85a5d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8a/309bb85a5d490011162d85db97d6efcd deleted file mode 100644 index 0f14d71..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8a/309bb85a5d490011162d85db97d6efcd +++ /dev/null @@ -1,481 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public void numberLeaf() { - numberLeaf(root); - } - - public void numberLeaf(BinaryNode node) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8a/90ba854d5a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8a/90ba854d5a490011162d85db97d6efcd deleted file mode 100644 index bdf02e8..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8a/90ba854d5a490011162d85db97d6efcd +++ /dev/null @@ -1,443 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - - while (iterator.hasNext()) { - E currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - currentNode.setData(currentObject); - - if (wing == 0) { - // Left Wing - - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8b/202f8972334d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/202f8972334d0011135ca7e1c9885acd new file mode 100644 index 0000000..8e41e06 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/202f8972334d0011135ca7e1c9885acd @@ -0,0 +1,77 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 { + head.next.prev = null; + head = head.next; + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8b/20435a372f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/20435a372f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..df92adb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/20435a372f4d0011135ca7e1c9885acd @@ -0,0 +1,36 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node tail = null; + private int size = 0; + + // Costruttore + public MyLinkedList() {} + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8b/a03359153c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/a03359153c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..91c7ed3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/a03359153c4d0011135ca7e1c9885acd @@ -0,0 +1,164 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8b/f0036ed559490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/f0036ed559490011162d85db97d6efcd deleted file mode 100644 index f45c81b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8b/f0036ed559490011162d85db97d6efcd +++ /dev/null @@ -1,433 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - BinaryNode currentNode = root; - - while (iterator.hasNext()) { - E currentObject = iterator.next(); - - currentNode.setData(currentObject); - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8b/f041deb77f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/f041deb77f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..13bf8a0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/f041deb77f4c00111d9cbe34e2b6027a @@ -0,0 +1,62 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; +import java.util.TreeSet; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8c/5059e604304d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/5059e604304d0011135ca7e1c9885acd new file mode 100644 index 0000000..d0acc3d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/5059e604304d0011135ca7e1c9885acd @@ -0,0 +1,55 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8c/60ea247043490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/60ea247043490011162d85db97d6efcd deleted file mode 100644 index 51d96a6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8c/60ea247043490011162d85db97d6efcd +++ /dev/null @@ -1,392 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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()); - flags.push(false); - } - - // visitiamo il nodo di sinistra infine - if (current.getLeft() != null) { - queueOfNodes.push(current.getLeft()); - flags.push(false); - } - } - } - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8c/a050684776460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/a050684776460011162d85db97d6efcd deleted file mode 100644 index b80efcd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8c/a050684776460011162d85db97d6efcd +++ /dev/null @@ -1,132 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8c/e0d57182024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/e0d57182024c00111d9cbe34e2b6027a deleted file mode 100644 index 4b7c71b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8c/e0d57182024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,9 +0,0 @@ -package jcf_set.exercise; - -public class IteratoreSenzaDuplicati { - - public static void main(String[] main) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/00701343094c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/00701343094c00111d9cbe34e2b6027a deleted file mode 100644 index 5938b3d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/00701343094c00111d9cbe34e2b6027a +++ /dev/null @@ -1,31 +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 aHashSet = new HashSet(Arrays.asList(aArray)); - Set bHashSet = new HashSet(Arrays.asList(bArray)); - - - - } - - // Operazioni di insiemistica - - /* - * Per eseguire una unione, è conveniente - * adoperare la Set poichè l'implementazione - * TreeSet permette di non mantenere duplicati. - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/10233d02034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/10233d02034c00111d9cbe34e2b6027a deleted file mode 100644 index c92cada..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/10233d02034c00111d9cbe34e2b6027a +++ /dev/null @@ -1,33 +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 lista = new ArrayList(); - - 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. - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/204922d15c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/204922d15c490011162d85db97d6efcd deleted file mode 100644 index 0010ebc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/204922d15c490011162d85db97d6efcd +++ /dev/null @@ -1,466 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - - } - - public void printLeaf(BinaryNode node) { - if (node == null) return; - - if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/6064b86d68460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/6064b86d68460011162d85db97d6efcd deleted file mode 100644 index 050ee6c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/6064b86d68460011162d85db97d6efcd +++ /dev/null @@ -1,58 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode getParent() { - return parent; - } - - public boolean isRoot() { - return parent == null; - } - - public boolean isLeftChild() { - return parent != null && parent.getLeft() == this; - if (parent != null && parent.getLeft() == this) return true; - return false; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/6068a86a30470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/6068a86a30470011162d85db97d6efcd deleted file mode 100644 index 32ac800..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/6068a86a30470011162d85db97d6efcd +++ /dev/null @@ -1,150 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/8028541534490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/8028541534490011162d85db97d6efcd deleted file mode 100644 index 07345d9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/8028541534490011162d85db97d6efcd +++ /dev/null @@ -1,272 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 il primo 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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()); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/c015358f68460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/c015358f68460011162d85db97d6efcd deleted file mode 100644 index 5dd854a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/c015358f68460011162d85db97d6efcd +++ /dev/null @@ -1,58 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode getParent() { - return parent; - } - - public boolean isRoot() { - return parent == null; - } - - public boolean isLeftChild() { - return parent != null && parent.getLeft() == this; - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/c0b488d773460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/c0b488d773460011162d85db97d6efcd deleted file mode 100644 index 811b6f2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/c0b488d773460011162d85db97d6efcd +++ /dev/null @@ -1,121 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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)) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8e/20287a7731470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8e/20287a7731470011162d85db97d6efcd deleted file mode 100644 index 87264f7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8e/20287a7731470011162d85db97d6efcd +++ /dev/null @@ -1,176 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8e/40dd24e33e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8e/40dd24e33e490011162d85db97d6efcd deleted file mode 100644 index 1359e59..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8e/40dd24e33e490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - if (current.getRight() != null) - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8e/a01c69eb31470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8e/a01c69eb31470011162d85db97d6efcd deleted file mode 100644 index de34a65..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8e/a01c69eb31470011162d85db97d6efcd +++ /dev/null @@ -1,189 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList arrayList = new ArrayList(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8e/e0c56954ac4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/8e/e0c56954ac4c0011135ca7e1c9885acd new file mode 100644 index 0000000..f3bfe55 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8e/e0c56954ac4c0011135ca7e1c9885acd @@ -0,0 +1,38 @@ +package jcf_map.exercise; + +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 int ContaDuplicati(BinaryNode node) { + int dup = 0; + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + + return mappa; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8f/001666176a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8f/001666176a460011162d85db97d6efcd deleted file mode 100644 index 1cd7900..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8f/001666176a460011162d85db97d6efcd +++ /dev/null @@ -1,78 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (this.parent == null) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8f/90085eafab4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/8f/90085eafab4c0011135ca7e1c9885acd new file mode 100644 index 0000000..c8785df --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8f/90085eafab4c0011135ca7e1c9885acd @@ -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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, TreeMap 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()) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8f/e065521c41490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8f/e065521c41490011162d85db97d6efcd deleted file mode 100644 index 470e278..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8f/e065521c41490011162d85db97d6efcd +++ /dev/null @@ -1,333 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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); - } - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9/0074cee833490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9/0074cee833490011162d85db97d6efcd deleted file mode 100644 index 5d3a61b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/9/0074cee833490011162d85db97d6efcd +++ /dev/null @@ -1,270 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 il primo 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - if (current.getData() != null) temporaryList.add(current.getData()); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9/80709bd965460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9/80709bd965460011162d85db97d6efcd deleted file mode 100644 index 5fe09db..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/9/80709bd965460011162d85db97d6efcd +++ /dev/null @@ -1,5 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9/f09fe899394d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/9/f09fe899394d0011135ca7e1c9885acd new file mode 100644 index 0000000..dccb934 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9/f09fe899394d0011135ca7e1c9885acd @@ -0,0 +1,134 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/90/209479787e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/90/209479787e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..4a06970 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/90/209479787e4c00111d9cbe34e2b6027a @@ -0,0 +1,49 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + for () { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/90/3065ab7235470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/90/3065ab7235470011162d85db97d6efcd deleted file mode 100644 index 2d33cc2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/90/3065ab7235470011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode current; - - queueOfNodes.add(node); - while(!queueOfNodes.isEmpty()) { - current = queueOfNodes.remove(); - - if (current.getData() != null) templist.add(current.getData); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/90/506ea2b582460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/90/506ea2b582460011162d85db97d6efcd deleted file mode 100644 index ff863b2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/90/506ea2b582460011162d85db97d6efcd +++ /dev/null @@ -1,57 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode node) { - if (node.getLeft() == null && node.getRight() == null) return 0; - - int nLeft = (node.getLeft() == null) ? 0 : node.getLeft().getSize(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/90/9001896c67460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/90/9001896c67460011162d85db97d6efcd deleted file mode 100644 index ccaf664..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/90/9001896c67460011162d85db97d6efcd +++ /dev/null @@ -1,26 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode right) { - this.data = data; - this.left = left; - } - - // METODI - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/90/b00a82fc2c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/90/b00a82fc2c470011162d85db97d6efcd deleted file mode 100644 index 7e80b87..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/90/b00a82fc2c470011162d85db97d6efcd +++ /dev/null @@ -1,103 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/90/e0e5fd013f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/90/e0e5fd013f490011162d85db97d6efcd deleted file mode 100644 index 976ecbc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/90/e0e5fd013f490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - if (current.getRight() != null) queueOfNodes.push(); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/91/000d53711b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/91/000d53711b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..535e090 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/91/000d53711b4d0011135ca7e1c9885acd @@ -0,0 +1,31 @@ +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 mappaAssociazione = new TreeMap(); + + companyMap.put("Rossi Marco", 8); + companyMap.put("Bianchi Luca", 14); + companyMap.put("Esposito Andrea", 6); + companyMap.put("Ferrari Matteo", 6); + companyMap.put("Romano Giulia", 14); + companyMap.put("Ricci Alessia", 6); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/91/30cef6bd81460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/91/30cef6bd81460011162d85db97d6efcd deleted file mode 100644 index 4b7e1ed..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/91/30cef6bd81460011162d85db97d6efcd +++ /dev/null @@ -1,45 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/91/400ac96f1e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/91/400ac96f1e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..357845c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/91/400ac96f1e4d0011135ca7e1c9885acd @@ -0,0 +1,24 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Data + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/91/401d4a2434490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/91/401d4a2434490011162d85db97d6efcd deleted file mode 100644 index 7083081..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/91/401d4a2434490011162d85db97d6efcd +++ /dev/null @@ -1,273 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 il primo 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/92/00a5a3503c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/92/00a5a3503c4600111c1cd5b6f02d115c deleted file mode 100644 index de1e3e2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/92/00a5a3503c4600111c1cd5b6f02d115c +++ /dev/null @@ -1,77 +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++) { - - } - } - - /** - * Funzione che ordina l'array - */ - public void ordina() { - for (int i = 0; i < dimensioneCorrente; i++) { - int minimo = i; - for (int j = i+1; j < dimensioneCorrente; j++) { - if () - if (vettore[minimo] > vettore[j]) minimo = j; - } - - } - } - - // FUNZIONI ASTRATTE - - /** - * Funzione che restituisce il valore della comparazione - */ - protected abstract int confronta(Object elemento1, Object elemento2); -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/92/60198f872f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/92/60198f872f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..08fe111 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/92/60198f872f4d0011135ca7e1c9885acd @@ -0,0 +1,48 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/92/60a3241932490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/92/60a3241932490011162d85db97d6efcd deleted file mode 100644 index 4b10be1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/92/60a3241932490011162d85db97d6efcd +++ /dev/null @@ -1,242 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator iterator() { - return iteratorPreOrder(); - } - - // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/93/105b76ae314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/93/105b76ae314600111c1cd5b6f02d115c deleted file mode 100644 index 1da0e36..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/93/105b76ae314600111c1cd5b6f02d115c +++ /dev/null @@ -1,27 +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; - } - - /** - * Funzione aggiungi - */ - public boolean aggiungi(Object elemento) { - if (elemento != null && dimensioneCorrente < dimensioneMassima) { - - dimensioneCorrente++; - return true; - } else return false; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/93/306782db7f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/93/306782db7f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..45789a3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/93/306782db7f4c00111d9cbe34e2b6027a @@ -0,0 +1,68 @@ +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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(new Comparator() { + @Override + public int compare(String s1, String s2) { + + } + }); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/93/30a18ad73f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/93/30a18ad73f490011162d85db97d6efcd deleted file mode 100644 index 4d70205..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/93/30a18ad73f490011162d85db97d6efcd +++ /dev/null @@ -1,314 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - Boolean flag; - - queueOfNodes.add(node); - flags.push(false); - - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - // Aggiungiamo il nodo di destra - if (current.getRight() != null) queueOfNodes.push(current.getRight()); - // Aggiungiamo - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/93/30d507c175460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/93/30d507c175460011162d85db97d6efcd deleted file mode 100644 index b8f22bf..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/93/30d507c175460011162d85db97d6efcd +++ /dev/null @@ -1,126 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || harRight() != otherType.hasRight()) return false; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/94/002be386084c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/94/002be386084c00111d9cbe34e2b6027a deleted file mode 100644 index b80924a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/94/002be386084c00111d9cbe34e2b6027a +++ /dev/null @@ -1,15 +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}; - Integer[] bArray = {1, 5, -6, -4, 5}; - - // Input come HashSet (Ordine non garantito) - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/94/10f11e9b6a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/94/10f11e9b6a460011162d85db97d6efcd deleted file mode 100644 index 6cdd316..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/94/10f11e9b6a460011162d85db97d6efcd +++ /dev/null @@ -1,88 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/94/308125c8034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/94/308125c8034c00111d9cbe34e2b6027a deleted file mode 100644 index b028808..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/94/308125c8034c00111d9cbe34e2b6027a +++ /dev/null @@ -1,50 +0,0 @@ -package jcf_set.exercise; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; - -public class IteratoreSenzaDuplicati { - - public static void main(String[] main) { - new IteratoreSenzaDuplicati().run(); - } - - public void run() { - - List lista = new ArrayList(); - - lista.add("tree"); - lista.add("flower"); - lista.add("tree"); - lista.add("flower"); - lista.add("animal"); - lista.add("flower"); - lista.add("fruit"); - - - - } - - // Esercizio 1 - /* - * Passare una lista con duplicati. - * Ottenere un iteratore privo di duplicati. - */ - private static > Iterator getIteratorNoDuplicates(Iterator it) { - HashSet tmp = new HashSet(); - while (it.hasNext()) tmp.add(it.next()); - return tmp.iterator(); - } - - // Esercizio 2 - /* - * Passare come parametro una lista con duplicati. - * Ottenere un iteratore senza duplicati e ordinato. - */ - private static > Iterator getIteratorNoDuplicatesOrdinated(){ - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/94/308c78f03a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/94/308c78f03a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..b2ac70f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/94/308c78f03a4d0011135ca7e1c9885acd @@ -0,0 +1,148 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/94/70730e55324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/94/70730e55324d0011135ca7e1c9885acd new file mode 100644 index 0000000..efde3f2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/94/70730e55324d0011135ca7e1c9885acd @@ -0,0 +1,67 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + newNode.next = head; + head = newNode; + } + size++; + } + + public E removeFirst() { + if (head == null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/94/b00dea3c3c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/94/b00dea3c3c4600111c1cd5b6f02d115c deleted file mode 100644 index c3c45d5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/94/b00dea3c3c4600111c1cd5b6f02d115c +++ /dev/null @@ -1,71 +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 - */ - - /** - * Funzione che ordina l'array - */ - public void ordina() { - for (int i = 0; i < dimensioneCorrente; i++) { - int minimo = i; - for (int j = i+1; j < dimensioneCorrente; j++) { - if () - if (vettore[minimo] > vettore[j]) minimo = j; - } - - } - } - - // FUNZIONI ASTRATTE - - /** - * Funzione che restituisce il valore della comparazione - */ - protected abstract int confronta(Object elemento1, Object elemento2); -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/94/d053c1c0ab4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/94/d053c1c0ab4c0011135ca7e1c9885acd new file mode 100644 index 0000000..f6a3f16 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/94/d053c1c0ab4c0011135ca7e1c9885acd @@ -0,0 +1,35 @@ +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, TreeMap 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(data, 1); + else mappa.put(data, mappa.get(data) + 1); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/94/f0f2ddce314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/94/f0f2ddce314600111c1cd5b6f02d115c deleted file mode 100644 index c08d0a3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/94/f0f2ddce314600111c1cd5b6f02d115c +++ /dev/null @@ -1,32 +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; - } - - /** - * 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 - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/95/10935ed373460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/95/10935ed373460011162d85db97d6efcd deleted file mode 100644 index 9ec57df..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/95/10935ed373460011162d85db97d6efcd +++ /dev/null @@ -1,120 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/95/40f5b996394d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/95/40f5b996394d0011135ca7e1c9885acd new file mode 100644 index 0000000..065af6a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/95/40f5b996394d0011135ca7e1c9885acd @@ -0,0 +1,133 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/95/50e19e643e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/95/50e19e643e490011162d85db97d6efcd deleted file mode 100644 index 7a38370..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/95/50e19e643e490011162d85db97d6efcd +++ /dev/null @@ -1,299 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryLisr) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/95/601a39ea2c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/95/601a39ea2c470011162d85db97d6efcd deleted file mode 100644 index 338599e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/95/601a39ea2c470011162d85db97d6efcd +++ /dev/null @@ -1,102 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/95/6091f6ec3a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/95/6091f6ec3a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..5174f24 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/95/6091f6ec3a4d0011135ca7e1c9885acd @@ -0,0 +1,147 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/95/d0608886084c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/95/d0608886084c00111d9cbe34e2b6027a deleted file mode 100644 index c54cfe7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/95/d0608886084c00111d9cbe34e2b6027a +++ /dev/null @@ -1,15 +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}; - Integer[] bArray = {1, 5, -6, -4, 5}; - - // Input come HashSet - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/95/d0aa950c35470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/95/d0aa950c35470011162d85db97d6efcd deleted file mode 100644 index 3a8a43b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/95/d0aa950c35470011162d85db97d6efcd +++ /dev/null @@ -1,222 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - - queueOfNodes.add(node); - - while(!queueOfNodes.isEmpty()) { - current = queueOfNodes.remove(); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/96/509f2670394d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/96/509f2670394d0011135ca7e1c9885acd new file mode 100644 index 0000000..c4922c4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/96/509f2670394d0011135ca7e1c9885acd @@ -0,0 +1,132 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/96/6004b92d3f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/96/6004b92d3f4600111c1cd5b6f02d115c deleted file mode 100644 index 8d721a2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/96/6004b92d3f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,31 +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 - */ - @Override - public boolean aggiungi(Object object) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/96/70bb02e532490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/96/70bb02e532490011162d85db97d6efcd deleted file mode 100644 index 85777ef..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/96/70bb02e532490011162d85db97d6efcd +++ /dev/null @@ -1,251 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator iterator() { - return iteratorPreOrder(); - } - - // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) - - protected void itpreorder(BinaryNode node, List temporaryList) { - Stack> queueOfNodes = new LinkedList>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - if - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/96/e00f21c1304600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/96/e00f21c1304600111c1cd5b6f02d115c deleted file mode 100644 index 50da30d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/96/e00f21c1304600111c1cd5b6f02d115c +++ /dev/null @@ -1,10 +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; - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/96/f03d263d7f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/96/f03d263d7f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..8ec016a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/96/f03d263d7f4c00111d9cbe34e2b6027a @@ -0,0 +1,53 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + for (String s : archivio.keySet()) { + Cliente currentCliente = archivio.get(s); + String currentCitta = currentCliente.cittaResidenza(); + + if (currentCitta.equals(citta)) autoSelezionate.put(s, ); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/96/f0bcb9b259490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/96/f0bcb9b259490011162d85db97d6efcd deleted file mode 100644 index a184175..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/96/f0bcb9b259490011162d85db97d6efcd +++ /dev/null @@ -1,430 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - - while (iterator.hasNext()) { - E current = iterator.next(); - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/103b2e5eac4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/97/103b2e5eac4c0011135ca7e1c9885acd new file mode 100644 index 0000000..131ba55 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/97/103b2e5eac4c0011135ca7e1c9885acd @@ -0,0 +1,42 @@ +package jcf_map.exercise; + +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 int ContaDuplicati(BinaryNode node) { + int dup = 0; + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + + for () { + + } + + return dup; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/a0779db233490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/97/a0779db233490011162d85db97d6efcd deleted file mode 100644 index d1b36c6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/97/a0779db233490011162d85db97d6efcd +++ /dev/null @@ -1,270 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 il primo 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.remove(); - - if (current) - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/b01cdcc881460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/97/b01cdcc881460011162d85db97d6efcd deleted file mode 100644 index 4844aaa..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/97/b01cdcc881460011162d85db97d6efcd +++ /dev/null @@ -1,46 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/c07480851b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/97/c07480851b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..0aaecff --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/97/c07480851b4d0011135ca7e1c9885acd @@ -0,0 +1,33 @@ +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 mappaAssociazione = new TreeMap(); + + companyMap.put("Rossi Marco", 8); + companyMap.put("Bianchi Luca", 14); + companyMap.put("Esposito Andrea", 6); + companyMap.put("Ferrari Matteo", 6); + companyMap.put("Romano Giulia", 14); + companyMap.put("Ricci Alessia", 6); + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/f050f7e65d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/97/f050f7e65d490011162d85db97d6efcd deleted file mode 100644 index 1c4f7b2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/97/f050f7e65d490011162d85db97d6efcd +++ /dev/null @@ -1,489 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public int numberLeaf() { - Integer number = 0; - numberLeaf(root, number); - return number; - } - - public void numberLeaf(BinaryNode node, Integer number) { - if (node == null) return; - - if (node.getLeft() == null && node.getRight() == null) number++; - else { - numberLeaf(node.getLeft(), number); - numberLeaf(node.getRight(), number); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/98/00c3eeef9b4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/98/00c3eeef9b4c0011135ca7e1c9885acd new file mode 100644 index 0000000..1db87ee --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/98/00c3eeef9b4c0011135ca7e1c9885acd @@ -0,0 +1,36 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + if (myList.isEmpty()) return true; + // Verifica che la lista si pari o dispari + HashMap valori = new HashMap(); + + Iterator it = myList.iterator(); + while (it.hasNext()) { + E current = it.next(); + + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/98/204d69a530470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/98/204d69a530470011162d85db97d6efcd deleted file mode 100644 index 7a7fbe9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/98/204d69a530470011162d85db97d6efcd +++ /dev/null @@ -1,162 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - - return - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/98/906ebb2130470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/98/906ebb2130470011162d85db97d6efcd deleted file mode 100644 index 9a48820..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/98/906ebb2130470011162d85db97d6efcd +++ /dev/null @@ -1,140 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - preorder(root, temporaryList); - return temporaryList.iterator(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/99/7090dd177f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/99/7090dd177f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..a563680 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/99/7090dd177f4c00111d9cbe34e2b6027a @@ -0,0 +1,52 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + for (String s : archivio.keySet()) { + Cliente currentCliente = archivio.get(s); + String current + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/99/b02246cc81460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/99/b02246cc81460011162d85db97d6efcd deleted file mode 100644 index ba3c5bc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/99/b02246cc81460011162d85db97d6efcd +++ /dev/null @@ -1,50 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9a/3059e0133f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/3059e0133f4600111c1cd5b6f02d115c deleted file mode 100644 index 9797ba0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/9a/3059e0133f4600111c1cd5b6f02d115c +++ /dev/null @@ -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 intero. - */ - public - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9a/80cb6e5d314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/80cb6e5d314600111c1cd5b6f02d115c deleted file mode 100644 index 834bd98..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/9a/80cb6e5d314600111c1cd5b6f02d115c +++ /dev/null @@ -1,23 +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; - } - - /** - * Funzione aggiungi - */ - public boolean aggiungi(Object oggetto) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9b/8024daf35a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9b/8024daf35a490011162d85db97d6efcd deleted file mode 100644 index 21f7152..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/9b/8024daf35a490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - newNode.setParent(currentNode); - - if (wing == 0) { - // Left Wing - currentNode. - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9b/809e7b322f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/9b/809e7b322f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..9865ad7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9b/809e7b322f4d0011135ca7e1c9885acd @@ -0,0 +1,37 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node tail = null; + private int size = 0; + + // Costruttore + public MyLinkedList() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9c/00fdca951b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/9c/00fdca951b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..6fa61b9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9c/00fdca951b4d0011135ca7e1c9885acd @@ -0,0 +1,34 @@ +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 mappaAssociazione = new TreeMap(); + + companyMap.put("Rossi Marco", 8); + companyMap.put("Bianchi Luca", 14); + companyMap.put("Esposito Andrea", 6); + companyMap.put("Ferrari Matteo", 6); + companyMap.put("Romano Giulia", 14); + companyMap.put("Ricci Alessia", 6); + + System.out.println("Stampa mappa associazione: "); + System.out.println(mappaAssociazione); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9c/b0e585eb2c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9c/b0e585eb2c470011162d85db97d6efcd deleted file mode 100644 index 7e80b87..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/9c/b0e585eb2c470011162d85db97d6efcd +++ /dev/null @@ -1,103 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9d/004b92023c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/9d/004b92023c4600111c1cd5b6f02d115c deleted file mode 100644 index 0b8301f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/9d/004b92023c4600111c1cd5b6f02d115c +++ /dev/null @@ -1,69 +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 ordina l'array - */ - public void ordina() { - for (int i = 0; i < dimensioneCorrente; i++) { - int minimo = i; - for (int j = i+1; j < dimensioneCorrente; j++) { - if () - if (vettore[minimo] > vettore[j]) minimo = j; - } - - } - } - - // FUNZIONI ASTRATTE - - /** - * Funzione che restituisce il valore della comparazione - */ - protected abstract int confronta(Object elemento1, Object elemento2) { - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9d/00a9a52a33470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9d/00a9a52a33470011162d85db97d6efcd deleted file mode 100644 index 19886a4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/9d/00a9a52a33470011162d85db97d6efcd +++ /dev/null @@ -1,205 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9d/f0dfea9e7a4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/9d/f0dfea9e7a4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..9f52d27 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9d/f0dfea9e7a4c00111d9cbe34e2b6027a @@ -0,0 +1,5 @@ +package parziale.p191108; + +public class Cliente { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9e/100192319b4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/9e/100192319b4c0011135ca7e1c9885acd new file mode 100644 index 0000000..15ee7c1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9e/100192319b4c0011135ca7e1c9885acd @@ -0,0 +1,26 @@ +package jcf_map.exercise; + +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9f/a03d9a6e41490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9f/a03d9a6e41490011162d85db97d6efcd deleted file mode 100644 index e20c637..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/9f/a03d9a6e41490011162d85db97d6efcd +++ /dev/null @@ -1,342 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - itinorder(root, temporaryList); - return temporaryList.iterator(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a/001619f39d4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/a/001619f39d4c0011135ca7e1c9885acd new file mode 100644 index 0000000..e3e6084 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a/001619f39d4c0011135ca7e1c9885acd @@ -0,0 +1,40 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + boolean dispari = true; + if (myList.isEmpty()) return dispari; + // Verifica che la lista si pari o dispari + HashMap valori = new HashMap(); + + Iterator 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); + } + + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a/004feec87d4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/a/004feec87d4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..fcbc2ce --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a/004feec87d4c00111d9cbe34e2b6027a @@ -0,0 +1,18 @@ +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) { + this.nominativo = nominativo; + this.cf = cf; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a/405cad2f5e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a/405cad2f5e490011162d85db97d6efcd deleted file mode 100644 index a01789c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a/405cad2f5e490011162d85db97d6efcd +++ /dev/null @@ -1,486 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 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 node) { - if (node == null) return 0; - - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a/b0fa8b836b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a/b0fa8b836b460011162d85db97d6efcd deleted file mode 100644 index 3c5aaf4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a/b0fa8b836b460011162d85db97d6efcd +++ /dev/null @@ -1,110 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode oldParent = parent; - parent = null; - - if (oldParent.left == this) oldParent.left = null; - else oldParent.right = null; - - return oldParent; - } - - public boolean hasLeft() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a/c01642c2034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/a/c01642c2034c00111d9cbe34e2b6027a deleted file mode 100644 index dbf6dac..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a/c01642c2034c00111d9cbe34e2b6027a +++ /dev/null @@ -1,48 +0,0 @@ -package jcf_set.exercise; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; - -public class IteratoreSenzaDuplicati { - - public static void main(String[] main) { - new IteratoreSenzaDuplicati().run(); - } - - public void run() { - - List lista = new ArrayList(); - - lista.add("tree"); - lista.add("flower"); - lista.add("tree"); - lista.add("flower"); - lista.add("animal"); - lista.add("flower"); - lista.add("fruit"); - - - - } - - // Esercizio 1 - /* - * Passare una lista con duplicati. - * Ottenere un iteratore privo di duplicati. - */ - private static > Iterator getIteratorNoDuplicates(Iterator it) { - HashSet tmp = new HashSet(); - while (it.hasNext()) tmp.add(it.next()); - return tmp.iterator(); - } - - // Esercizio 2 - /* - * Passare come parametro una lista con duplicati. - * Ottenere un iteratore senza duplicati e ordinato. - */ - private static > Iterator - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a/e01254345c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a/e01254345c490011162d85db97d6efcd deleted file mode 100644 index d22ccff..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a/e01254345c490011162d85db97d6efcd +++ /dev/null @@ -1,450 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a0/107553371e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/a0/107553371e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..62423f1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a0/107553371e4d0011135ca7e1c9885acd @@ -0,0 +1,16 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Data + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a0/20039e585e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a0/20039e585e490011162d85db97d6efcd deleted file mode 100644 index 0edd29d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a0/20039e585e490011162d85db97d6efcd +++ /dev/null @@ -1,487 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 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 node) { - if (node == null) return 0; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.hasRight() == null) ? 0 : numberLeaf(node.getRight()); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a0/80cda19b5b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a0/80cda19b5b490011162d85db97d6efcd deleted file mode 100644 index 8f6d995..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a0/80cda19b5b490011162d85db97d6efcd +++ /dev/null @@ -1,446 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a0/a06c94983e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a0/a06c94983e490011162d85db97d6efcd deleted file mode 100644 index b3de34a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a0/a06c94983e490011162d85db97d6efcd +++ /dev/null @@ -1,303 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(current); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a0/f01ef9ce7a4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/a0/f01ef9ce7a4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..9fa5c09 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a0/f01ef9ce7a4c00111d9cbe34e2b6027a @@ -0,0 +1,12 @@ +package parziale.p191108; + +public class Cliente { + + // Variabili di instanza + private String nominativo; + private String cf; + private String cittaResidenza; + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/00aa53841a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/00aa53841a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..92574db --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/00aa53841a4d0011135ca7e1c9885acd @@ -0,0 +1,5 @@ +package jcf_map.exercise; + +public class CompanyMap { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/40a27dfd3b4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/40a27dfd3b4600111c1cd5b6f02d115c deleted file mode 100644 index 8ee0b4b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/40a27dfd3b4600111c1cd5b6f02d115c +++ /dev/null @@ -1,65 +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 ordina l'array - */ - public void ordina() { - for (int i = 0; i < dimensioneCorrente; i++) { - int minimo = i; - for (int j = i+1; j < dimensioneCorrente; j++) { - if (vettore[minimo] > vettore[j]) minimo = j; - } - } - } - - // FUNZIONI ASTRATTE - - /** - * Funzione che restituisce il valore della comparazione - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/6023ce1c32490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/6023ce1c32490011162d85db97d6efcd deleted file mode 100644 index 941e8d7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/6023ce1c32490011162d85db97d6efcd +++ /dev/null @@ -1,245 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator iterator() { - return iteratorPreOrder(); - } - - // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) - - protected void preorder() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/706e6f1c9c4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/706e6f1c9c4c0011135ca7e1c9885acd new file mode 100644 index 0000000..53eca66 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/706e6f1c9c4c0011135ca7e1c9885acd @@ -0,0 +1,40 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + boolean dispari = true; + if (myList.isEmpty()) return true; + // Verifica che la lista si pari o dispari + HashMap valori = new HashMap(); + + Iterator 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); + } + + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/b0b7f7e8354d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/b0b7f7e8354d0011135ca7e1c9885acd new file mode 100644 index 0000000..da3bd30 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/b0b7f7e8354d0011135ca7e1c9885acd @@ -0,0 +1,91 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/f01e02445a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/f01e02445a490011162d85db97d6efcd deleted file mode 100644 index bb5a4ba..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/f01e02445a490011162d85db97d6efcd +++ /dev/null @@ -1,441 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - BinaryNode currentNode = root; - E currentObject - - while (iterator.hasNext()) { - E currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - currentNode.setData(currentObject); - - if (wing == 0) { - // Left Wing - - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a2/60b567d142490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a2/60b567d142490011162d85db97d6efcd deleted file mode 100644 index b5a7a0c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a2/60b567d142490011162d85db97d6efcd +++ /dev/null @@ -1,373 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - Boolean flag; - - queueOfNodes.push(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - flag = flags.pop(); - - if (flag) { - // il nodo è da visitare - } else { - // il nodo non è ancora da visitare - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a2/9085d8fa3e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a2/9085d8fa3e490011162d85db97d6efcd deleted file mode 100644 index 8c662cc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a2/9085d8fa3e490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - if (current.getRight() != null) queueOfNodes.add(); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/20c867805c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/20c867805c490011162d85db97d6efcd deleted file mode 100644 index 5d785c6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/20c867805c490011162d85db97d6efcd +++ /dev/null @@ -1,457 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf(); - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/40499cd540490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/40499cd540490011162d85db97d6efcd deleted file mode 100644 index caf134a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/40499cd540490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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); - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/6073d5943f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/6073d5943f490011162d85db97d6efcd deleted file mode 100644 index e1a40b6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/6073d5943f490011162d85db97d6efcd +++ /dev/null @@ -1,310 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - // Aggiungiamo il nodo di destra - if (current.getRight() != null) queueOfNodes.push(current.getRight()); - // Aggiungiamo - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/d08010f173460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/d08010f173460011162d85db97d6efcd deleted file mode 100644 index d9b9577..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/d08010f173460011162d85db97d6efcd +++ /dev/null @@ -1,121 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a4/501420ef59490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a4/501420ef59490011162d85db97d6efcd deleted file mode 100644 index 35d2910..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a4/501420ef59490011162d85db97d6efcd +++ /dev/null @@ -1,434 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - BinaryNode currentNode = root; - - while (iterator.hasNext()) { - E currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - currentNode.setData(currentObject); - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a4/a01aada535470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a4/a01aada535470011162d85db97d6efcd deleted file mode 100644 index 5979f1a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a4/a01aada535470011162d85db97d6efcd +++ /dev/null @@ -1,225 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode current; - - queueOfNodes.add(node); - while(!queueOfNodes.isEmpty()) { - current = queueOfNodes.remove(); - - if (current.getData() != null) temporaryList.add(current.getData()); - if () - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a4/d06318c5a94c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/a4/d06318c5a94c0011135ca7e1c9885acd new file mode 100644 index 0000000..383ea19 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a4/d06318c5a94c0011135ca7e1c9885acd @@ -0,0 +1,16 @@ +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. + * 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 Map ContaDuplicati() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a4/d0ee7f743d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a4/d0ee7f743d490011162d85db97d6efcd deleted file mode 100644 index 737a562..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a4/d0ee7f743d490011162d85db97d6efcd +++ /dev/null @@ -1,280 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - } - - // ITERATOR - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a5/102120144f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a5/102120144f490011162d85db97d6efcd deleted file mode 100644 index 708efbf..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a5/102120144f490011162d85db97d6efcd +++ /dev/null @@ -1,398 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a5/504ccd2a43490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a5/504ccd2a43490011162d85db97d6efcd deleted file mode 100644 index b8e170c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a5/504ccd2a43490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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) { - - } - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a5/d0540f3b5e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a5/d0540f3b5e490011162d85db97d6efcd deleted file mode 100644 index 61bcc33..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a5/d0540f3b5e490011162d85db97d6efcd +++ /dev/null @@ -1,486 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 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 node) { - if (node == null) return 0; - - int nLeft = (node.hasLeft() == null) ? 0 : numberLeaf(node.getLeft()); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a5/f0c825f459490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a5/f0c825f459490011162d85db97d6efcd deleted file mode 100644 index 890a21f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a5/f0c825f459490011162d85db97d6efcd +++ /dev/null @@ -1,435 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - BinaryNode currentNode = root; - - while (iterator.hasNext()) { - E currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - currentNode.setData(currentObject); - - if (wing == 0) - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a6/2096ffbf374600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/2096ffbf374600111c1cd5b6f02d115c deleted file mode 100644 index c99e1a0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a6/2096ffbf374600111c1cd5b6f02d115c +++ /dev/null @@ -1,40 +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; - } - - /** - * 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[dimensioneCorrente]; - } else return null; - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a6/c0d906da3a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/c0d906da3a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..40868ee --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/c0d906da3a4d0011135ca7e1c9885acd @@ -0,0 +1,146 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a6/d0de4159804c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/d0de4159804c00111d9cbe34e2b6027a new file mode 100644 index 0000000..a59dc54 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/d0de4159804c00111d9cbe34e2b6027a @@ -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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(archivio); + return archivioOrdinato; + } + + // 6. Stampa su video l'archivio attuale + public void stampaArchivio() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a6/e0c3cd033f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/e0c3cd033f490011162d85db97d6efcd deleted file mode 100644 index 95fd9b0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a6/e0c3cd033f490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - if (current.getRight() != null) queueOfNodes.push(current.getRight()); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a6/f0821ecc30470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/f0821ecc30470011162d85db97d6efcd deleted file mode 100644 index 904412b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a6/f0821ecc30470011162d85db97d6efcd +++ /dev/null @@ -1,164 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a7/a019eca23a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/a7/a019eca23a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..f097c87 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a7/a019eca23a4d0011135ca7e1c9885acd @@ -0,0 +1,145 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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; + } + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a7/b05fc276a94c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/a7/b05fc276a94c0011135ca7e1c9885acd new file mode 100644 index 0000000..623bca4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a7/b05fc276a94c0011135ca7e1c9885acd @@ -0,0 +1,12 @@ +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. + * 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) + */ +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a7/c0e72fcb31470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a7/c0e72fcb31470011162d85db97d6efcd deleted file mode 100644 index 84b3e4f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a7/c0e72fcb31470011162d85db97d6efcd +++ /dev/null @@ -1,189 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a7/e05e8f653b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/a7/e05e8f653b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..074d5ee --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a7/e05e8f653b4d0011135ca7e1c9885acd @@ -0,0 +1,152 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node nextNode = prevNode.next; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/10bd6d4d3c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/10bd6d4d3c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..6150546 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/10bd6d4d3c4d0011135ca7e1c9885acd @@ -0,0 +1,166 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/20f25a831a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/20f25a831a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..5ee46d6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/20f25a831a4d0011135ca7e1c9885acd @@ -0,0 +1,5 @@ +package jcf_map.exercise; + +public class CompanyMap { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/3021b38c5a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/3021b38c5a490011162d85db97d6efcd deleted file mode 100644 index 395fc2e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/3021b38c5a490011162d85db97d6efcd +++ /dev/null @@ -1,444 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - currentNode.setData(currentObject); - - if (wing == 0) { - // Left Wing - - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/306c514e2f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/306c514e2f470011162d85db97d6efcd deleted file mode 100644 index f63453b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/306c514e2f470011162d85db97d6efcd +++ /dev/null @@ -1,129 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/30e19efa1e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/30e19efa1e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..7a3a6fc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/30e19efa1e4d0011135ca7e1c9885acd @@ -0,0 +1,30 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/505d9cba5a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/505d9cba5a490011162d85db97d6efcd deleted file mode 100644 index a1e5ef6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/505d9cba5a490011162d85db97d6efcd +++ /dev/null @@ -1,444 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(); - - if (wing == 0) { - // Left Wing - - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/b02e0c337f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/b02e0c337f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..a9f149e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/b02e0c337f4c00111d9cbe34e2b6027a @@ -0,0 +1,53 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + for (String s : archivio.keySet()) { + Cliente currentCliente = archivio.get(s); + String currentCitta = currentCliente.cittaResidenza(); + + if (currentCitta.equals()) + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/c068c59f3b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/c068c59f3b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..c34adde --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/c068c59f3b4d0011135ca7e1c9885acd @@ -0,0 +1,156 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node nextNode = prevNode.next; + + prevNode.next = newNode; + newNode.prev = prevNode; + newNode.next = nextNode; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/d02469f259490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/d02469f259490011162d85db97d6efcd deleted file mode 100644 index cf4a1d0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/d02469f259490011162d85db97d6efcd +++ /dev/null @@ -1,435 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - BinaryNode currentNode = root; - - while (iterator.hasNext()) { - E currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - currentNode.setData(currentObject); - - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a9/105caa031e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/a9/105caa031e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..6adc77a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a9/105caa031e4d0011135ca7e1c9885acd @@ -0,0 +1,5 @@ +package list.mylinkedlist; + +public class MyLinkedList { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a9/3038f2b7084c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/a9/3038f2b7084c00111d9cbe34e2b6027a deleted file mode 100644 index 53b347a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a9/3038f2b7084c00111d9cbe34e2b6027a +++ /dev/null @@ -1,19 +0,0 @@ -package jcf_set.exercise; - -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 aHashSet = new HashSet(Arrays.asList(aArray)); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a9/60d22ed51a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/a9/60d22ed51a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..dfa8433 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a9/60d22ed51a4d0011135ca7e1c9885acd @@ -0,0 +1,11 @@ +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. + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a9/f0f8b5b76a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a9/f0f8b5b76a460011162d85db97d6efcd deleted file mode 100644 index 50b7705..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a9/f0f8b5b76a460011162d85db97d6efcd +++ /dev/null @@ -1,94 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldrRight; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/40d151256b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/40d151256b460011162d85db97d6efcd deleted file mode 100644 index 10de7fa..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/40d151256b460011162d85db97d6efcd +++ /dev/null @@ -1,105 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode oldParent = parent; - parent = null; - - - - return oldParent; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/b0ba55f56a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/b0ba55f56a460011162d85db97d6efcd deleted file mode 100644 index 5ebc451..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/b0ba55f56a460011162d85db97d6efcd +++ /dev/null @@ -1,98 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/c05006351e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/c05006351e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..ed24e75 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/c05006351e4d0011135ca7e1c9885acd @@ -0,0 +1,14 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/c068bd71354d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/c068bd71354d0011135ca7e1c9885acd new file mode 100644 index 0000000..e745a33 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/c068bd71354d0011135ca7e1c9885acd @@ -0,0 +1,85 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node secondPlace = head.next; + + head = secondPlace; + secondPlace.prev = null; + firstPlace.next = null; + } + size--; + return tmp; + } + + public E getLast() { + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/c0f74d4032490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/c0f74d4032490011162d85db97d6efcd deleted file mode 100644 index 77581a5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/c0f74d4032490011162d85db97d6efcd +++ /dev/null @@ -1,245 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator iterator() { - return iteratorPreOrder(); - } - - // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) - - protected void itpreorder() { - Queue> queueOfNodes = new LinkedList>(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/d05bc382034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/d05bc382034c00111d9cbe34e2b6027a deleted file mode 100644 index 08f384f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/d05bc382034c00111d9cbe34e2b6027a +++ /dev/null @@ -1,41 +0,0 @@ -package jcf_set.exercise; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; - -public class IteratoreSenzaDuplicati { - - public static void main(String[] main) { - new IteratoreSenzaDuplicati().run(); - } - - public void run() { - - List lista = new ArrayList(); - - 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 Iterator getIteratorNoDuplicates(Iterator it) { - HashSet tmp = new HashSet(); - while (it.hasNext()) tmp.add(it.next()); - return tmp.iterator(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/e0b98c806a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/e0b98c806a460011162d85db97d6efcd deleted file mode 100644 index efec2cc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/e0b98c806a460011162d85db97d6efcd +++ /dev/null @@ -1,82 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - return oldLeft; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ab/4061129f3c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/ab/4061129f3c4600111c1cd5b6f02d115c deleted file mode 100644 index 2e264ec..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ab/4061129f3c4600111c1cd5b6f02d115c +++ /dev/null @@ -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 (int j = i+1; j < dimensioneCorrente; j++) { - if (confronta(vettore[minimo], vettore[j]) > 0) minimo = j; - } - int temp = vettore[minimo]; - vettore[minimo] = vettore[j]; - vett - } - } - - // FUNZIONI ASTRATTE - - /** - * Funzione che restituisce il valore della comparazione - */ - protected abstract int confronta(Object elemento1, Object elemento2); -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ab/9040ba755e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ab/9040ba755e490011162d85db97d6efcd deleted file mode 100644 index 907b057..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ab/9040ba755e490011162d85db97d6efcd +++ /dev/null @@ -1,490 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 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 node) { - if (node == null) return 0; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - if () - - return n - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ab/f09ec7bf73460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ab/f09ec7bf73460011162d85db97d6efcd deleted file mode 100644 index 79573ad..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ab/f09ec7bf73460011162d85db97d6efcd +++ /dev/null @@ -1,119 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/10ec5f3331470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/10ec5f3331470011162d85db97d6efcd deleted file mode 100644 index 1a71674..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/10ec5f3331470011162d85db97d6efcd +++ /dev/null @@ -1,168 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - inorder(root, temporaryList); - return temporaryList.iterator(); - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/701902fc75460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/701902fc75460011162d85db97d6efcd deleted file mode 100644 index d16933d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/701902fc75460011162d85db97d6efcd +++ /dev/null @@ -1,126 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/d011a72a314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/d011a72a314600111c1cd5b6f02d115c deleted file mode 100644 index 0c1be37..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/d011a72a314600111c1cd5b6f02d115c +++ /dev/null @@ -1,15 +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) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/d0db81781e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/d0db81781e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..7cae160 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/d0db81781e4d0011135ca7e1c9885acd @@ -0,0 +1,26 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Data + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node() { + + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/e083cf3caa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/e083cf3caa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..ec9925d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/e083cf3caa4c0011135ca7e1c9885acd @@ -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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + if (node == null) return mappa; + + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/f01b6fc6324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/f01b6fc6324d0011135ca7e1c9885acd new file mode 100644 index 0000000..732351f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/f01b6fc6324d0011135ca7e1c9885acd @@ -0,0 +1,78 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + newNode.next = head; + head = newNode; + } + size++; + } + + public E removeFirst() { + if (head == null) throw new NoSuchElementException(); + E tmp; + if (size == 1) { + tmp = head.data; + head = tail = null; + return tmp; + } else { + + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/10570af73f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/10570af73f4600111c1cd5b6f02d115c deleted file mode 100644 index 1c371ab..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/10570af73f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,81 +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; - this.dimensioneCorrente = 0; - this.vettore = new Object[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[i]; - vettore[i] = temp; - } - } - - // FUNZIONI ASTRATTE - - /** - * Funzione che restituisce il valore della comparazione - */ - protected abstract int confronta(Object elemento1, Object elemento2); -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/20b1b4175d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/20b1b4175d490011162d85db97d6efcd deleted file mode 100644 index 8655999..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/20b1b4175d490011162d85db97d6efcd +++ /dev/null @@ -1,469 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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()); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/90c3fd0136470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/90c3fd0136470011162d85db97d6efcd deleted file mode 100644 index 7bf5745..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/90c3fd0136470011162d85db97d6efcd +++ /dev/null @@ -1,231 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder(){ - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/a088c5217e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/a088c5217e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..b5c60d7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/a088c5217e4c00111d9cbe34e2b6027a @@ -0,0 +1,37 @@ +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) { + 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); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/a0a1b8d12b470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/a0a1b8d12b470011162d85db97d6efcd deleted file mode 100644 index f8c0212..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/a0a1b8d12b470011162d85db97d6efcd +++ /dev/null @@ -1,92 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/d065d9013d4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/d065d9013d4600111c1cd5b6f02d115c deleted file mode 100644 index e30a8e8..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/d065d9013d4600111c1cd5b6f02d115c +++ /dev/null @@ -1,80 +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; - this.vettore = new Object[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[i]; - vettore[i] = temp; - } - } - - // FUNZIONI ASTRATTE - - /** - * Funzione che restituisce il valore della comparazione - */ - protected abstract int confronta(Object elemento1, Object elemento2); -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ae/20bcc8d9364d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/ae/20bcc8d9364d0011135ca7e1c9885acd new file mode 100644 index 0000000..6133419 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ae/20bcc8d9364d0011135ca7e1c9885acd @@ -0,0 +1,98 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 { + + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ae/9078c40968460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ae/9078c40968460011162d85db97d6efcd deleted file mode 100644 index e82c75b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ae/9078c40968460011162d85db97d6efcd +++ /dev/null @@ -1,48 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode getParent() { - return parent; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ae/a0c7558e1e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/ae/a0c7558e1e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..f485d71 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ae/a0c7558e1e4d0011135ca7e1c9885acd @@ -0,0 +1,28 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Data + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ae/e0541f4d6b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ae/e0541f4d6b460011162d85db97d6efcd deleted file mode 100644 index 8a7ca24..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ae/e0541f4d6b460011162d85db97d6efcd +++ /dev/null @@ -1,105 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode oldParent = parent; - parent = null; - - if () - - return oldParent; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/af/20c1761533490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/af/20c1761533490011162d85db97d6efcd deleted file mode 100644 index a43f8d9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/af/20c1761533490011162d85db97d6efcd +++ /dev/null @@ -1,252 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator iterator() { - return iteratorPreOrder(); - } - - // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) - - protected void itpreorder(BinaryNode node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - if - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/af/307c879a324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/af/307c879a324d0011135ca7e1c9885acd new file mode 100644 index 0000000..d02f5c2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/af/307c879a324d0011135ca7e1c9885acd @@ -0,0 +1,71 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + newNode.next = head; + head = newNode; + } + size++; + } + + public E removeFirst() { + if (head == null) throw new NoSuchElementException(); + if (size == 1) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/af/70f588e09b4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/af/70f588e09b4c0011135ca7e1c9885acd new file mode 100644 index 0000000..a4051bd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/af/70f588e09b4c0011135ca7e1c9885acd @@ -0,0 +1,32 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + if (myList.isEmpty()) return true; + // Verifica che la lista si pari o dispari + HashMap valori = new HashMap(); + + Iterator it = myList.iterator(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/af/b0c0e5362f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/af/b0c0e5362f4600111c1cd5b6f02d115c deleted file mode 100644 index 38237d9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/af/b0c0e5362f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,7 +0,0 @@ -package test; - -public class Main { - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/af/d0f560f45c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/af/d0f560f45c490011162d85db97d6efcd deleted file mode 100644 index 858ec19..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/af/d0f560f45c490011162d85db97d6efcd +++ /dev/null @@ -1,469 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(); - } - - public void printLeaf(BinaryNode 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()); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/af/f08afe2a42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/af/f08afe2a42490011162d85db97d6efcd deleted file mode 100644 index 91ee426..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/af/f08afe2a42490011162d85db97d6efcd +++ /dev/null @@ -1,352 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b/50b5671567460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b/50b5671567460011162d85db97d6efcd deleted file mode 100644 index 7ffc9d5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b/50b5671567460011162d85db97d6efcd +++ /dev/null @@ -1,20 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORE - - public BinaryNode(E data) { - - } - - // METODI - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b/90ce6b235d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b/90ce6b235d490011162d85db97d6efcd deleted file mode 100644 index ffd7ea0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b/90ce6b235d490011162d85db97d6efcd +++ /dev/null @@ -1,473 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b/a0df51aa5b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b/a0df51aa5b490011162d85db97d6efcd deleted file mode 100644 index 079aab5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b/a0df51aa5b490011162d85db97d6efcd +++ /dev/null @@ -1,446 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b/e0db97b4034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/b/e0db97b4034c00111d9cbe34e2b6027a deleted file mode 100644 index c163f27..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b/e0db97b4034c00111d9cbe34e2b6027a +++ /dev/null @@ -1,48 +0,0 @@ -package jcf_set.exercise; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; - -public class IteratoreSenzaDuplicati { - - public static void main(String[] main) { - new IteratoreSenzaDuplicati().run(); - } - - public void run() { - - List lista = new ArrayList(); - - lista.add("tree"); - lista.add("flower"); - lista.add("tree"); - lista.add("flower"); - lista.add("animal"); - lista.add("flower"); - lista.add("fruit"); - - - - } - - // Esercizio 1 - /* - * Passare una lista con duplicati. - * Ottenere un iteratore privo di duplicati. - */ - private static > Iterator getIteratorNoDuplicates(Iterator it) { - HashSet tmp = new HashSet(); - while (it.hasNext()) tmp.add(it.next()); - return tmp.iterator(); - } - - // Esercizio 2 - /* - * Passare come parametro una lista con duplicati. - * Ottenere un iteratore senza duplicati e ordinato. - */ - private static Iterator - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b/f0ecd9421b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/b/f0ecd9421b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..e054d44 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b/f0ecd9421b4d0011135ca7e1c9885acd @@ -0,0 +1,23 @@ +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. + * + */ + + TreeMap<> + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b0/00e2d1c242490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b0/00e2d1c242490011162d85db97d6efcd deleted file mode 100644 index 5a45c57..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b0/00e2d1c242490011162d85db97d6efcd +++ /dev/null @@ -1,369 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - Boolean flag; - - queueOfNodes.push(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - flag = flags.pop(); - - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b0/904b3fab3a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/b0/904b3fab3a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..7b270d4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b0/904b3fab3a4d0011135ca7e1c9885acd @@ -0,0 +1,145 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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; + } + + Node newNode = new Node(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b0/a0154147314d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/b0/a0154147314d0011135ca7e1c9885acd new file mode 100644 index 0000000..7605e40 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b0/a0154147314d0011135ca7e1c9885acd @@ -0,0 +1,59 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b0/e0aee249aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/b0/e0aee249aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..7eb4918 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b0/e0aee249aa4c0011135ca7e1c9885acd @@ -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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + if (node == null) return mappa; + + if (node.getLeft() != null) ContaDuplicati(node.getLeft(), ); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b0/e0b08c9931470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b0/e0b08c9931470011162d85db97d6efcd deleted file mode 100644 index ac5aaf7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b0/e0b08c9931470011162d85db97d6efcd +++ /dev/null @@ -1,180 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/1030922b2c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/1030922b2c470011162d85db97d6efcd deleted file mode 100644 index 8a69756..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/1030922b2c470011162d85db97d6efcd +++ /dev/null @@ -1,94 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/30f7f1cf82460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/30f7f1cf82460011162d85db97d6efcd deleted file mode 100644 index d1b9629..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/30f7f1cf82460011162d85db97d6efcd +++ /dev/null @@ -1,60 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode node) { - if (node.getLeft() == null && node.getRight() == null) return 0; - - int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/60ddd8ea7d4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/60ddd8ea7d4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..55cef2b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/60ddd8ea7d4c00111d9cbe34e2b6027a @@ -0,0 +1,24 @@ +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) { + this.nominativo = nominativo; + this.cf = cf; + this.cittaResidenza = cittaResidenza; + } + + // Getters + public String nominativo () { + return nominativo; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/708b2b285b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/708b2b285b490011162d85db97d6efcd deleted file mode 100644 index 1ddf445..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/708b2b285b490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - //newNode.setParent(currentNode); - - if (wing == 0) { - // Left Wing - newNode.setParentAsLeftChild(); - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/90e17a76084c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/90e17a76084c00111d9cbe34e2b6027a deleted file mode 100644 index 0328a7e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/90e17a76084c00111d9cbe34e2b6027a +++ /dev/null @@ -1,15 +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}; - Integer[] bArray = {1, 5, -6, -4, 5}; - - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/c0ae0fee35470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/c0ae0fee35470011162d85db97d6efcd deleted file mode 100644 index 083d5d2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/c0ae0fee35470011162d85db97d6efcd +++ /dev/null @@ -1,227 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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()); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/f00cf77d404600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/f00cf77d404600111c1cd5b6f02d115c deleted file mode 100644 index d4af5c7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/f00cf77d404600111c1cd5b6f02d115c +++ /dev/null @@ -1,9 +0,0 @@ -package vettore_ordinabile; - -public class Main { - - public static void main (String[] Args) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b2/20d7be8d31470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b2/20d7be8d31470011162d85db97d6efcd deleted file mode 100644 index 983f259..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b2/20d7be8d31470011162d85db97d6efcd +++ /dev/null @@ -1,180 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b2/20ffaf3f76460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b2/20ffaf3f76460011162d85db97d6efcd deleted file mode 100644 index 26c1513..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b2/20ffaf3f76460011162d85db97d6efcd +++ /dev/null @@ -1,132 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b2/306384d376460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b2/306384d376460011162d85db97d6efcd deleted file mode 100644 index fa16ccc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b2/306384d376460011162d85db97d6efcd +++ /dev/null @@ -1,146 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - - while (cur.parent != null){ - d++; - cur = cur.parent; - } - - return d; - } - - public int height() { - if (left == null && right == null) return 0; - - int hLeft = (left == null) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b2/40ccf410354d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/b2/40ccf410354d0011135ca7e1c9885acd new file mode 100644 index 0000000..9fa7f5d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b2/40ccf410354d0011135ca7e1c9885acd @@ -0,0 +1,81 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node secondPlace = head.next; + + head = secondPlace; + secondPlace.prev = null; + firstPlace.next = null; + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b2/f04311e7024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/b2/f04311e7024c00111d9cbe34e2b6027a deleted file mode 100644 index fce3af6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b2/f04311e7024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,26 +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 lista = new ArrayList(); - - lista.add("tree"); - lista.add("flower"); - lista.add("tree"); - lista.add("flower"); - lista.add("animal"); - lista.add("flower"); - lista.add("fruit"); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b3/2011fa8959490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b3/2011fa8959490011162d85db97d6efcd deleted file mode 100644 index 4a0ac48..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b3/2011fa8959490011162d85db97d6efcd +++ /dev/null @@ -1,429 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - - while (iterator.hasNext()) { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b3/40a0f8b2024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/b3/40a0f8b2024c00111d9cbe34e2b6027a deleted file mode 100644 index 30ddb74..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b3/40a0f8b2024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,17 +0,0 @@ -package jcf_set.exercise; - -import java.util.List; - -public class IteratoreSenzaDuplicati { - - public static void main(String[] main) { - new IteratoreSenzaDuplicati().run(); - } - - public void run() { - - List lista = new ArrayList - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b3/907284b773460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b3/907284b773460011162d85db97d6efcd deleted file mode 100644 index bd40471..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b3/907284b773460011162d85db97d6efcd +++ /dev/null @@ -1,116 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b4/2004e3a3374d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/b4/2004e3a3374d0011135ca7e1c9885acd new file mode 100644 index 0000000..2b4ec16 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b4/2004e3a3374d0011135ca7e1c9885acd @@ -0,0 +1,114 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node newNode = newNode; + 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 firstPlace = head; + Node 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 lastNode = tail; + Node 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(); + if (size == 0) { + head = tail = new Node(item, null, null); + } else { + Node newNode = new Node(); + } + size++; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b4/40c6b52d43490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b4/40c6b52d43490011162d85db97d6efcd deleted file mode 100644 index 22d1ec7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b4/40c6b52d43490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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(); - } - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b4/50a02ee6ab4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/b4/50a02ee6ab4c0011135ca7e1c9885acd new file mode 100644 index 0000000..431f4b5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b4/50a02ee6ab4c0011135ca7e1c9885acd @@ -0,0 +1,38 @@ +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 Map ContaDuplicati(BinaryNode node) { + TreeMap mappa = new TreeMap(); + + } + + protected static Map ContaDuplicati(BinaryNode node, TreeMap 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(data, 1); + else mappa.put(data, mappa.get(data) + 1); + + return mappa; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b4/70331b5f67460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b4/70331b5f67460011162d85db97d6efcd deleted file mode 100644 index 44b2203..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b4/70331b5f67460011162d85db97d6efcd +++ /dev/null @@ -1,21 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORE - - public BinaryNode(E data) { - this.data = data; - left = right = parent = null; - } - - // METODI - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b5/70084afa344d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/b5/70084afa344d0011135ca7e1c9885acd new file mode 100644 index 0000000..ab2dc78 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b5/70084afa344d0011135ca7e1c9885acd @@ -0,0 +1,81 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node secondPlace = head.next; + + head = secondPlace; + secondPlace.prev = null; + + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b5/906502ac1b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/b5/906502ac1b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..07ba29d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b5/906502ac1b4d0011135ca7e1c9885acd @@ -0,0 +1,38 @@ +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 + */ + + + + public void run() { + TreeMap mappaAssociazione = new TreeMap(); + + mappaAssociazione.put("Rossi Marco", 8); + mappaAssociazione.put("Bianchi Luca", 14); + mappaAssociazione.put("Esposito Andrea", 6); + mappaAssociazione.put("Ferrari Matteo", 6); + mappaAssociazione.put("Romano Giulia", 14); + mappaAssociazione.put("Ricci Alessia", 6); + + System.out.println("Stampa mappa associazione: "); + System.out.println(mappaAssociazione); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b5/b0f2484943490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b5/b0f2484943490011162d85db97d6efcd deleted file mode 100644 index ef9d421..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b5/b0f2484943490011162d85db97d6efcd +++ /dev/null @@ -1,389 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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()); - flags.push(false); - } - - // visitiamo il nodo di sinistra infine - if (current.getLeft() != null) { - - } - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/103f6a46ac4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/103f6a46ac4c0011135ca7e1c9885acd new file mode 100644 index 0000000..8b5f0cf --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/103f6a46ac4c0011135ca7e1c9885acd @@ -0,0 +1,36 @@ +package jcf_map.exercise; + +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 Map ContaDuplicati(BinaryNode node) { + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + return mappa; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/20ba920e34490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/20ba920e34490011162d85db97d6efcd deleted file mode 100644 index 528b5eb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/20ba920e34490011162d85db97d6efcd +++ /dev/null @@ -1,272 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 il primo 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 () - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/4045d59a67460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/4045d59a67460011162d85db97d6efcd deleted file mode 100644 index 6306a24..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/4045d59a67460011162d85db97d6efcd +++ /dev/null @@ -1,28 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode right) { - this.data = data; - this.left = left; - this.right = right; - this.parent = null; - } - - // METODI - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/50faa40f9e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/50faa40f9e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..8b8e299 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/50faa40f9e4c0011135ca7e1c9885acd @@ -0,0 +1,41 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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); + } + + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/60f2e7655a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/60f2e7655a490011162d85db97d6efcd deleted file mode 100644 index 393050e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/60f2e7655a490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - currentNode.setData(currentObject); - - if (wing == 0) { - // Left Wing - - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/b061ffd23a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/b061ffd23a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..d54b833 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/b061ffd23a4d0011135ca7e1c9885acd @@ -0,0 +1,146 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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; + } + + Node newNode = new Node(item, null, null); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b7/00e150ce59490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b7/00e150ce59490011162d85db97d6efcd deleted file mode 100644 index db43d65..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b7/00e150ce59490011162d85db97d6efcd +++ /dev/null @@ -1,431 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - BinaryNode currentNode = root; - - while (iterator.hasNext()) { - E currentObject = iterator.next(); - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b7/30cb4ab9384d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/b7/30cb4ab9384d0011135ca7e1c9885acd new file mode 100644 index 0000000..8d2fa0a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b7/30cb4ab9384d0011135ca7e1c9885acd @@ -0,0 +1,121 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node oldLastNode = tail; + oldLastNode.next = newNode; + newNode.prev = oldLastNode; + tail = newNode; + } + size++; + } + + public void clear() { + head = tail = null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b7/4054c4472f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b7/4054c4472f470011162d85db97d6efcd deleted file mode 100644 index 2bceb3d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b7/4054c4472f470011162d85db97d6efcd +++ /dev/null @@ -1,127 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b8/2033ec633e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/2033ec633e490011162d85db97d6efcd deleted file mode 100644 index a575884..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b8/2033ec633e490011162d85db97d6efcd +++ /dev/null @@ -1,299 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b8/6052de592f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/6052de592f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..45c93d7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/6052de592f4d0011135ca7e1c9885acd @@ -0,0 +1,40 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node tail = null; + private int size = 0; + + // Costruttore + public MyLinkedList() {} + + // Metodi + + public boolean isEmpty() { + return size == 0; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b8/70bf12691e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/70bf12691e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..7eb4c8f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/70bf12691e4d0011135ca7e1c9885acd @@ -0,0 +1,21 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Data + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b8/900dc62440490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/900dc62440490011162d85db97d6efcd deleted file mode 100644 index 2b835ed..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b8/900dc62440490011162d85db97d6efcd +++ /dev/null @@ -1,319 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 () - } else { - // il nodo non è da visitare - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b8/f082ea0a034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/f082ea0a034c00111d9cbe34e2b6027a deleted file mode 100644 index dfed9d6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b8/f082ea0a034c00111d9cbe34e2b6027a +++ /dev/null @@ -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 lista = new ArrayList(); - - 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. - */ - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b9/301798c52f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b9/301798c52f470011162d85db97d6efcd deleted file mode 100644 index 766e667..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b9/301798c52f470011162d85db97d6efcd +++ /dev/null @@ -1,136 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b9/a05160b77a4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/b9/a05160b77a4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..413bf62 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b9/a05160b77a4c00111d9cbe34e2b6027a @@ -0,0 +1,8 @@ +package parziale.p191108; + +public class Cliente { + + // Variabili di instanza + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b9/b0ce8fb82f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/b9/b0ce8fb82f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..c7da99f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b9/b0ce8fb82f4d0011135ca7e1c9885acd @@ -0,0 +1,54 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ba/00b03b8359490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/00b03b8359490011162d85db97d6efcd deleted file mode 100644 index 4a0ac48..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ba/00b03b8359490011162d85db97d6efcd +++ /dev/null @@ -1,429 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - - while (iterator.hasNext()) { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ba/305cdf5b42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/305cdf5b42490011162d85db97d6efcd deleted file mode 100644 index 11c1e45..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ba/305cdf5b42490011162d85db97d6efcd +++ /dev/null @@ -1,354 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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. - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ba/30ab24567e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/30ab24567e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..d58bfed --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/30ab24567e4c00111d9cbe34e2b6027a @@ -0,0 +1,46 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ba/e05cec0669460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/e05cec0669460011162d85db97d6efcd deleted file mode 100644 index 70c5127..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ba/e05cec0669460011162d85db97d6efcd +++ /dev/null @@ -1,69 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left == null) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/007503241e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/007503241e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..b868b49 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/007503241e4d0011135ca7e1c9885acd @@ -0,0 +1,10 @@ +package list.mylinkedlist; + +public class MyLinkedList { + + /* + * Classe Nodo + */ + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/40f291a467460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/40f291a467460011162d85db97d6efcd deleted file mode 100644 index 8e4d3ec..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/40f291a467460011162d85db97d6efcd +++ /dev/null @@ -1,33 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode right) { - this.data = data; - - this.left = left; - this.right = right; - - this.parent = null; - - if (this.left == null) - } - - // METODI - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/80b4cbd167460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/80b4cbd167460011162d85db97d6efcd deleted file mode 100644 index c1c20de..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/80b4cbd167460011162d85db97d6efcd +++ /dev/null @@ -1,40 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/90fe1ea53c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/90fe1ea53c4600111c1cd5b6f02d115c deleted file mode 100644 index 3186d0e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/90fe1ea53c4600111c1cd5b6f02d115c +++ /dev/null @@ -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; - } - int temp = vettore[minimo]; - vettore[minimo] = vettore[j]; - vett - } - } - - // FUNZIONI ASTRATTE - - /** - * Funzione che restituisce il valore della comparazione - */ - protected abstract int confronta(Object elemento1, Object elemento2); -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/c0302eb131470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/c0302eb131470011162d85db97d6efcd deleted file mode 100644 index b4899d4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/c0302eb131470011162d85db97d6efcd +++ /dev/null @@ -1,184 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/c0ab2dd92f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/c0ab2dd92f470011162d85db97d6efcd deleted file mode 100644 index d539bfd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/c0ab2dd92f470011162d85db97d6efcd +++ /dev/null @@ -1,139 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - preorder(root, temporaryList); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/f03128b93b4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/f03128b93b4600111c1cd5b6f02d115c deleted file mode 100644 index 8c35b44..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/f03128b93b4600111c1cd5b6f02d115c +++ /dev/null @@ -1,62 +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 ordina l'array - */ - public void ordina() { - for (int i = 0; i < dimensioneCorrente; i++) { - int minimo = i; - } - } - - // FUNZIONI ASTRATTE - - /** - * Funzione che restituisce il valore della comparazione - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bc/109a57395b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/109a57395b490011162d85db97d6efcd deleted file mode 100644 index bd32729..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bc/109a57395b490011162d85db97d6efcd +++ /dev/null @@ -1,446 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - //newNode.setParent(currentNode); - - if (wing == 0) { - // Left Wing - newNode.setParentAsLeftChild(currentNode); - } else { - // Right Wing - newNode.setParentAsRightChild(currentNode); - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bc/10e8bdc1024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/10e8bdc1024c00111d9cbe34e2b6027a deleted file mode 100644 index bdc60d7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bc/10e8bdc1024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,21 +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 lista = new ArrayList(); - - lista.add("tree"); - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bc/203da234314d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/203da234314d0011135ca7e1c9885acd new file mode 100644 index 0000000..30f3698 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/203da234314d0011135ca7e1c9885acd @@ -0,0 +1,59 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bc/700f177e5e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/700f177e5e490011162d85db97d6efcd deleted file mode 100644 index 54291ac..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bc/700f177e5e490011162d85db97d6efcd +++ /dev/null @@ -1,492 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 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 node) { - if (node == null) return 0; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - int odd; - - - - return n - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bc/b0fb6ef6354d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/b0fb6ef6354d0011135ca7e1c9885acd new file mode 100644 index 0000000..561147c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/b0fb6ef6354d0011135ca7e1c9885acd @@ -0,0 +1,92 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/20eadfba384d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/20eadfba384d0011135ca7e1c9885acd new file mode 100644 index 0000000..befc509 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/20eadfba384d0011135ca7e1c9885acd @@ -0,0 +1,122 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node oldLastNode = tail; + oldLastNode.next = newNode; + newNode.prev = oldLastNode; + tail = newNode; + } + size++; + } + + public void clear() { + head = tail = null; + size = 0; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/40743bd32f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/40743bd32f470011162d85db97d6efcd deleted file mode 100644 index 3622f9a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/40743bd32f470011162d85db97d6efcd +++ /dev/null @@ -1,138 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - preorder(root, temporaryList); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/50f67209804c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/50f67209804c00111d9cbe34e2b6027a new file mode 100644 index 0000000..b9073de --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/50f67209804c00111d9cbe34e2b6027a @@ -0,0 +1,68 @@ +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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(new Comparator() { + @Override + public int compare(String s1, String s2) { + return s1.compareTo(s2); + } + }); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/a01434bd59490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/a01434bd59490011162d85db97d6efcd deleted file mode 100644 index 9864268..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/a01434bd59490011162d85db97d6efcd +++ /dev/null @@ -1,430 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - - while (iterator.hasNext()) { - E currentObject = iterator.next(); - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/c09bfca4374600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/c09bfca4374600111c1cd5b6f02d115c deleted file mode 100644 index 83574da..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/c09bfca4374600111c1cd5b6f02d115c +++ /dev/null @@ -1,38 +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; - } - - /** - * 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[dimensioneCorrente]; - } else return null; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/f018ccf668460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/f018ccf668460011162d85db97d6efcd deleted file mode 100644 index b632132..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/f018ccf668460011162d85db97d6efcd +++ /dev/null @@ -1,68 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/f0e1f12f82460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/f0e1f12f82460011162d85db97d6efcd deleted file mode 100644 index e6a69c1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/f0e1f12f82460011162d85db97d6efcd +++ /dev/null @@ -1,53 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/be/805c67fa36470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/be/805c67fa36470011162d85db97d6efcd deleted file mode 100644 index 50091ab..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/be/805c67fa36470011162d85db97d6efcd +++ /dev/null @@ -1,232 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/10c8f1c068460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/10c8f1c068460011162d85db97d6efcd deleted file mode 100644 index 6291744..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/10c8f1c068460011162d85db97d6efcd +++ /dev/null @@ -1,64 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 elem) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/10d12b183b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/10d12b183b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..2e2a8c4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/10d12b183b4d0011135ca7e1c9885acd @@ -0,0 +1,150 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index -1 ; i++) { + prevNode = prevNode.next; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/d07e3d442c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/d07e3d442c470011162d85db97d6efcd deleted file mode 100644 index aa9a2db..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/d07e3d442c470011162d85db97d6efcd +++ /dev/null @@ -1,96 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode resNode; - resNode = find(targetElement, root.getLeft()); - if () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/d093a61e38470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/d093a61e38470011162d85db97d6efcd deleted file mode 100644 index a47cdee..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/d093a61e38470011162d85db97d6efcd +++ /dev/null @@ -1,237 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c/305b0ebc3c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c/305b0ebc3c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..4cd4767 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c/305b0ebc3c4d0011135ca7e1c9885acd @@ -0,0 +1,175 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + return currentNode.data; + } + + public E set(int index, E item) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + + Node currentNode = head; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c/30cb312643490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c/30cb312643490011162d85db97d6efcd deleted file mode 100644 index 61fd767..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c/30cb312643490011162d85db97d6efcd +++ /dev/null @@ -1,380 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c/506904f2ab4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c/506904f2ab4c0011135ca7e1c9885acd new file mode 100644 index 0000000..d22bcfa --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c/506904f2ab4c0011135ca7e1c9885acd @@ -0,0 +1,39 @@ +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 Map ContaDuplicati(BinaryNode node) { + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + return mappa; + } + + protected static Map ContaDuplicati(BinaryNode node, TreeMap 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(data, 1); + else mappa.put(data, mappa.get(data) + 1); + + return mappa; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c/b0ff8da7034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/c/b0ff8da7034c00111d9cbe34e2b6027a deleted file mode 100644 index b1e0043..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c/b0ff8da7034c00111d9cbe34e2b6027a +++ /dev/null @@ -1,48 +0,0 @@ -package jcf_set.exercise; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; - -public class IteratoreSenzaDuplicati { - - public static void main(String[] main) { - new IteratoreSenzaDuplicati().run(); - } - - public void run() { - - List lista = new ArrayList(); - - lista.add("tree"); - lista.add("flower"); - lista.add("tree"); - lista.add("flower"); - lista.add("animal"); - lista.add("flower"); - lista.add("fruit"); - - - - } - - // Esercizio 1 - /* - * Passare una lista con duplicati. - * Ottenere un iteratore privo di duplicati. - */ - private static Iterator getIteratorNoDuplicates(Iterator it) { - HashSet tmp = new HashSet(); - while (it.hasNext()) tmp.add(it.next()); - return tmp.iterator(); - } - - // Esercizio 2 - /* - * Passare come parametro una lista con duplicati. - * Ottenere un iteratore senza duplicati e ordinato. - */ - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c0/10c476b9024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/10c476b9024c00111d9cbe34e2b6027a deleted file mode 100644 index 9dc50dd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c0/10c476b9024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,19 +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 lista = new ArrayList(); - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c0/2039934e3d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/2039934e3d490011162d85db97d6efcd deleted file mode 100644 index a21165f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c0/2039934e3d490011162d85db97d6efcd +++ /dev/null @@ -1,276 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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()); - } - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c0/20af39739e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/20af39739e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..cd3ee40 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/20af39739e4c0011135ca7e1c9885acd @@ -0,0 +1,45 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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; + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c0/505e349c314d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/505e349c314d0011135ca7e1c9885acd new file mode 100644 index 0000000..f7b6838 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/505e349c314d0011135ca7e1c9885acd @@ -0,0 +1,60 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c0/509703fd7d4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/509703fd7d4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..d197955 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/509703fd7d4c00111d9cbe34e2b6027a @@ -0,0 +1,32 @@ +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) { + 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; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c0/609b89707f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/609b89707f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..4f3639e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/609b89707f4c00111d9cbe34e2b6027a @@ -0,0 +1,56 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili; +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c0/90795f935d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/90795f935d490011162d85db97d6efcd deleted file mode 100644 index 56b153d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c0/90795f935d490011162d85db97d6efcd +++ /dev/null @@ -1,487 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public void numberLeaf() { - Integer number = 0; - numberLeaf(root, number); - } - - public void numberLeaf(BinaryNode node, Integer number) { - if (node == null) return; - - if (node.getLeft() == null && node.getRight() == null) number++; - else { - numberLeaf(); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c0/90cca37b2f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/90cca37b2f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..fa7ea8b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/90cca37b2f4d0011135ca7e1c9885acd @@ -0,0 +1,48 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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) + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c0/a05f1dc81b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/a05f1dc81b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..66a8a00 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/a05f1dc81b4d0011135ca7e1c9885acd @@ -0,0 +1,42 @@ +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 + */ + + public static void main(String[] main) { + new CompanyMap().run(); + } + + public void run() { + TreeMap mappaAssociazione = new TreeMap(); + + mappaAssociazione.put("Rossi Marco", 8); + mappaAssociazione.put("Bianchi Luca", 14); + mappaAssociazione.put("Esposito Andrea", 6); + mappaAssociazione.put("Ferrari Matteo", 6); + mappaAssociazione.put("Romano Giulia", 14); + mappaAssociazione.put("Ricci Alessia", 6); + + System.out.println("Stampa mappa associazione: "); + System.out.println(mappaAssociazione); + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c0/f0531afb384d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/f0531afb384d0011135ca7e1c9885acd new file mode 100644 index 0000000..51e7a60 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/f0531afb384d0011135ca7e1c9885acd @@ -0,0 +1,128 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c1/10ee30a37a4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/10ee30a37a4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..c4c2111 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/10ee30a37a4c00111d9cbe34e2b6027a @@ -0,0 +1,8 @@ +package parziale.p191108; + +public class Cliente { + + // Variabili di classe + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c1/30f532133f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/30f532133f490011162d85db97d6efcd deleted file mode 100644 index 229767d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c1/30f532133f490011162d85db97d6efcd +++ /dev/null @@ -1,309 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - // Aggiungiamo il nodo di destra - if (current.getRight() != null) queueOfNodes.push(current.getRight()); - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c1/501c44aa324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/501c44aa324d0011135ca7e1c9885acd new file mode 100644 index 0000000..5938a4b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/501c44aa324d0011135ca7e1c9885acd @@ -0,0 +1,72 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + newNode.next = head; + head = newNode; + } + size++; + } + + public E removeFirst() { + if (head == null) throw new NoSuchElementException(); + if (size == 1) { + Node tmp = head; + head = tail = null; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c1/600c34831e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/600c34831e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..6955e86 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/600c34831e4d0011135ca7e1c9885acd @@ -0,0 +1,26 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Data + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c1/70c507362f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/70c507362f470011162d85db97d6efcd deleted file mode 100644 index 0c7e93d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c1/70c507362f470011162d85db97d6efcd +++ /dev/null @@ -1,123 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 - */ - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c1/80f508da9e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/80f508da9e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..60b1451 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/80f508da9e4c0011135ca7e1c9885acd @@ -0,0 +1,49 @@ +package jcf_map.exercise; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari> { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/604e84eb84460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/604e84eb84460011162d85db97d6efcd deleted file mode 100644 index d5a1115..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/604e84eb84460011162d85db97d6efcd +++ /dev/null @@ -1,67 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - return leftTree; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/80f86a3a2f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/80f86a3a2f470011162d85db97d6efcd deleted file mode 100644 index 977e3b1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/80f86a3a2f470011162d85db97d6efcd +++ /dev/null @@ -1,125 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/a0e545d9384d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/a0e545d9384d0011135ca7e1c9885acd new file mode 100644 index 0000000..0e74232 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/a0e545d9384d0011135ca7e1c9885acd @@ -0,0 +1,124 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node oldLastNode = tail; + oldLastNode.next = newNode; + newNode.prev = oldLastNode; + tail = newNode; + } + size++; + } + + public void clear() { + head = tail = null; + size = 0; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/c01cbcea3f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/c01cbcea3f490011162d85db97d6efcd deleted file mode 100644 index 3bd9963..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/c01cbcea3f490011162d85db97d6efcd +++ /dev/null @@ -1,315 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - Boolean flag; - - queueOfNodes.add(node); - flags.push(false); - - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - flag = flags.pop(); - - // Aggiungiamo il nodo di destra - if (current.getRight() != null) queueOfNodes.push(current.getRight()); - // Aggiungiamo - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c3/209b77457f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/c3/209b77457f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..d02f625 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c3/209b77457f4c00111d9cbe34e2b6027a @@ -0,0 +1,53 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + for (String s : archivio.keySet()) { + Cliente currentCliente = archivio.get(s); + String currentCitta = currentCliente.cittaResidenza(); + + if (currentCitta.equals(citta)) autoSelezionate.put(s, currentCliente); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c3/50ce1dbca94c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c3/50ce1dbca94c0011135ca7e1c9885acd new file mode 100644 index 0000000..ac046d0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c3/50ce1dbca94c0011135ca7e1c9885acd @@ -0,0 +1,14 @@ +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. + * 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) + */ + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c3/c04425fc68460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c3/c04425fc68460011162d85db97d6efcd deleted file mode 100644 index 86a7a58..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c3/c04425fc68460011162d85db97d6efcd +++ /dev/null @@ -1,68 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c4/50badc144f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c4/50badc144f490011162d85db97d6efcd deleted file mode 100644 index 45898f5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c4/50badc144f490011162d85db97d6efcd +++ /dev/null @@ -1,408 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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. - */ - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c4/60ac830650490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c4/60ac830650490011162d85db97d6efcd deleted file mode 100644 index 2067b6a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c4/60ac830650490011162d85db97d6efcd +++ /dev/null @@ -1,409 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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. - */ - public LinkedBinaryTree() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c4/9051f9251e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c4/9051f9251e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..ea2f709 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c4/9051f9251e4d0011135ca7e1c9885acd @@ -0,0 +1,12 @@ +package list.mylinkedlist; + +implements java.array:list; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c5/301f79bd30470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c5/301f79bd30470011162d85db97d6efcd deleted file mode 100644 index f342625..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c5/301f79bd30470011162d85db97d6efcd +++ /dev/null @@ -1,160 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c5/f02cda398f4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c5/f02cda398f4c0011135ca7e1c9885acd new file mode 100644 index 0000000..db627c9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c5/f02cda398f4c0011135ca7e1c9885acd @@ -0,0 +1,19 @@ +package jcf_map.exercise; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c6/b05e6a5f84460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c6/b05e6a5f84460011162d85db97d6efcd deleted file mode 100644 index 0afbd8f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c6/b05e6a5f84460011162d85db97d6efcd +++ /dev/null @@ -1,65 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/1091642840490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/1091642840490011162d85db97d6efcd deleted file mode 100644 index 43c013b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/1091642840490011162d85db97d6efcd +++ /dev/null @@ -1,319 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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(); - } else { - // il nodo non è da visitare - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/50a913d13f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/50a913d13f490011162d85db97d6efcd deleted file mode 100644 index bb05eff..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/50a913d13f490011162d85db97d6efcd +++ /dev/null @@ -1,313 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - - queueOfNodes.add(node); - flags.push(false); - - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - // Aggiungiamo il nodo di destra - if (current.getRight() != null) queueOfNodes.push(current.getRight()); - // Aggiungiamo - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/9076f0e0ab4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/9076f0e0ab4c0011135ca7e1c9885acd new file mode 100644 index 0000000..07cc2ff --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/9076f0e0ab4c0011135ca7e1c9885acd @@ -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 Map ContaDuplicati(BinaryNode node) { + TreeMap mappa = new TreeMap(); + } + + protected static Map ContaDuplicati(BinaryNode node, TreeMap 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(data, 1); + else mappa.put(data, mappa.get(data) + 1); + + return mappa; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/d086203880460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/d086203880460011162d85db97d6efcd deleted file mode 100644 index 2759159..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/d086203880460011162d85db97d6efcd +++ /dev/null @@ -1,23 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - - // METODI - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c8/10d8ddcf34470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/10d8ddcf34470011162d85db97d6efcd deleted file mode 100644 index 6893ab8..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c8/10d8ddcf34470011162d85db97d6efcd +++ /dev/null @@ -1,219 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - - queueOfNodes(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c8/20f9929f40490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/20f9929f40490011162d85db97d6efcd deleted file mode 100644 index b17e595..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c8/20f9929f40490011162d85db97d6efcd +++ /dev/null @@ -1,321 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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) - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c8/90e52692aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/90e52692aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..be5c95e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/90e52692aa4c0011135ca7e1c9885acd @@ -0,0 +1,33 @@ +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap 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 () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c8/a0c8bb7042490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/a0c8bb7042490011162d85db97d6efcd deleted file mode 100644 index a4ead15..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c8/a0c8bb7042490011162d85db97d6efcd +++ /dev/null @@ -1,357 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c8/f0cca80c804c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/f0cca80c804c00111d9cbe34e2b6027a new file mode 100644 index 0000000..d6e227a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/f0cca80c804c00111d9cbe34e2b6027a @@ -0,0 +1,68 @@ +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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(new Comparator() { + @Override + public int compare(String s1, String s2) { + return s1.compareTo(s2); + } + }); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c9/0002c31c41490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/0002c31c41490011162d85db97d6efcd deleted file mode 100644 index e65d4d3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c9/0002c31c41490011162d85db97d6efcd +++ /dev/null @@ -1,336 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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); - } - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c9/00275a1d43490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/00275a1d43490011162d85db97d6efcd deleted file mode 100644 index ecc412e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c9/00275a1d43490011162d85db97d6efcd +++ /dev/null @@ -1,378 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 - queueOfNodes.push(current); - flags.push(true); - - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c9/0046b2b6384d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/0046b2b6384d0011135ca7e1c9885acd new file mode 100644 index 0000000..681e580 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/0046b2b6384d0011135ca7e1c9885acd @@ -0,0 +1,121 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node oldLastNode = tail; + oldLastNode.next = newNode; + newNode.prev = oldLastNode; + tail = newNode; + } + size++; + } + + public void clear() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c9/605ae6a0414d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/605ae6a0414d0011135ca7e1c9885acd new file mode 100644 index 0000000..628b392 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/605ae6a0414d0011135ca7e1c9885acd @@ -0,0 +1,170 @@ +package parziale.p251110; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.TreeMap; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + ListIterator lit = pazienti.listIterator(); + while (lit.hasNext()) { + Paziente current = lit.next(); + int cmp = current.compareTo(paziente); + if (cmp > 0) { + lit.previous(); + lit.add(paziente); + return true; + } + } + + lit.add(paziente); + return true; + } + + // 3. Dimissione + public Paziente dimettiPaziente(String id, int annoNascita) { + if (id == null || annoNascita <= 0) return null; + Paziente tmp = new Paziente(id, annoNascita); + + Iterator iterator = pazienti.iterator(); + while (iterator.hasNext()) { + Paziente current = iterator.next(); + int cmp = current.compareTo(tmp); + if (cmp == 0) { + // Paziente da rimuovere + iterator.remove(); + return current; + } + if (cmp > 0) return null; + } + + return null; + } + + // 4. Età media + public int etaMedia() { + if (pazienti.isEmpty()) return 0; + + int currentDate = LocalDate.now().getYear(); + int avg = 0; + + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + int ageYear = currentDate - it.next().getAnnoNascita(); + avg += ageYear; + } + + return avg / pazienti.size(); + } + + // 5. Età più rappresenta + public int etaMediaPiuRappresentata() { + if (pazienti.isEmpty()) return 0; + + int currentDate = LocalDate.now().getYear(); + TreeMap listaEta = new TreeMap(); + + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + int currentAge = currentDate - current.getAnnoNascita(); + + if (listaEta.get(currentAge) == null) listaEta.put(currentAge, 1); + else listaEta.put(currentAge, listaEta.get(currentAge) + 1); + } + + int maxAge = 0; + int maxFrequency = 0; + + for (Integer i : listaEta.keySet()) { + int currentFrequency = listaEta.get(i); + + if (currentFrequency > maxFrequency) { + maxFrequency = currentFrequency; + maxAge = i; + } + + } + + return maxAge; + } + + // 6. Ricoverati ordinati per codice + public ArrayList ricoveratiOrdinatiPerCodice() { + ArrayList ricoverati = new ArrayList(pazienti); + + ricoverati.sort(new Comparator() { + @Override + public int compare(Paziente p1, Paziente p2) { + String id1 = p1.getId(); + String id2 = p2.getId(); + return id1.compareTo(id2); + } + }); + + return ricoverati; + } + + // 7. Distribuzione pazienti per anno di nascita + public Map pazientiPerAnnoDiNascita() { + // AnnoNascita, NumeroPazienti + Map pazientiPerAnnoDiNascita = new TreeMap(); // Si usa TreeMap per avere una vista già ordinata di anni + + if (pazienti.isEmpty()) return pazientiPerAnnoDiNascita; + + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + int annoNascita = current.getAnnoNascita(); + + if (pazientiPerAnnoDiNascita.get(annoNascita) == null) pazientiPerAnnoDiNascita.put(annoNascita, 1); + else pazientiPerAnnoDiNascita.put(annoNascita, pazientiPerAnnoDiNascita.get(annoNascita) + 1); + } + + return pazientiPerAnnoDiNascita; + } + + public static List ordinaListaClinicaPerNumeroPazienti(List cliniche) { + if (cliniche == null) throw new NullPointerException(); + + cliniche.sort(new Comparator() { + @Override + public int compare(Clinica c1, Clinica c2) { + int pazienti1 = c1.pazienti.size(); + int pazienti2 = c2.pazienti.size(); + return pazienti1 - pazienti2; + } + }); + + return cliniche; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c9/606c6432314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/606c6432314600111c1cd5b6f02d115c deleted file mode 100644 index 9ab69fb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c9/606c6432314600111c1cd5b6f02d115c +++ /dev/null @@ -1,15 +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"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ca/20a2e12f2e470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ca/20a2e12f2e470011162d85db97d6efcd deleted file mode 100644 index 7f3eba9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ca/20a2e12f2e470011162d85db97d6efcd +++ /dev/null @@ -1,114 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - if (temp != null) { - temp.setData(null); - return true; - } - return false; - } - - public boolean contains(E targetElement) { - return find(targetElement, root) != null; - } - - // ITERATORI - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ca/501dff395e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ca/501dff395e490011162d85db97d6efcd deleted file mode 100644 index 296cd4b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ca/501dff395e490011162d85db97d6efcd +++ /dev/null @@ -1,486 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 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 node) { - if (node == null) return 0; - - int nLeft = (node.hasLeft() == null) ? 0 : numberLeaf(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ca/60fbbc12304d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/ca/60fbbc12304d0011135ca7e1c9885acd new file mode 100644 index 0000000..c3bfb72 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ca/60fbbc12304d0011135ca7e1c9885acd @@ -0,0 +1,59 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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) { + + } else { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ca/d06b013d74460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ca/d06b013d74460011162d85db97d6efcd deleted file mode 100644 index a2e6510..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ca/d06b013d74460011162d85db97d6efcd +++ /dev/null @@ -1,125 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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() - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cb/20589ef92e4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/cb/20589ef92e4600111c1cd5b6f02d115c deleted file mode 100644 index cc4eaa5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cb/20589ef92e4600111c1cd5b6f02d115c +++ /dev/null @@ -1,7 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=21 -org.eclipse.jdt.core.compiler.compliance=21 -org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled -org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning -org.eclipse.jdt.core.compiler.release=enabled -org.eclipse.jdt.core.compiler.source=21 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cb/b0ae5c367e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/cb/b0ae5c367e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..ba2d9d6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cb/b0ae5c367e4c00111d9cbe34e2b6027a @@ -0,0 +1,44 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cb/d0c4efbb7a4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/cb/d0c4efbb7a4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..c9fe900 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cb/d0c4efbb7a4c00111d9cbe34e2b6027a @@ -0,0 +1,10 @@ +package parziale.p191108; + +public class Cliente { + + // Variabili di instanza + private String nominativo; + private String cf; + private String cittaResidenza; + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cb/e02a8003364d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/cb/e02a8003364d0011135ca7e1c9885acd new file mode 100644 index 0000000..e7fb7d6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cb/e02a8003364d0011135ca7e1c9885acd @@ -0,0 +1,92 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cb/e05b733c334d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/cb/e05b733c334d0011135ca7e1c9885acd new file mode 100644 index 0000000..0cc777f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cb/e05b733c334d0011135ca7e1c9885acd @@ -0,0 +1,76 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 { + head.next = null; + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/107e382585460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/107e382585460011162d85db97d6efcd deleted file mode 100644 index c78ff0a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/107e382585460011162d85db97d6efcd +++ /dev/null @@ -1,71 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - - return leftTree; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/70abe1d485460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/70abe1d485460011162d85db97d6efcd deleted file mode 100644 index 91cf0cc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/70abe1d485460011162d85db97d6efcd +++ /dev/null @@ -1,78 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/70f59fc0354d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/70f59fc0354d0011135ca7e1c9885acd new file mode 100644 index 0000000..32e1658 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/70f59fc0354d0011135ca7e1c9885acd @@ -0,0 +1,90 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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) return new SuchElementException(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/80ffbe6f1b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/80ffbe6f1b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..c23e7db --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/80ffbe6f1b4d0011135ca7e1c9885acd @@ -0,0 +1,26 @@ +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 mappaAssociazione = new TreeMap(); + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/f01161f7374d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/f01161f7374d0011135ca7e1c9885acd new file mode 100644 index 0000000..ad554cb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/f01161f7374d0011135ca7e1c9885acd @@ -0,0 +1,114 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + + } + size++; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/003f4abb35470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/003f4abb35470011162d85db97d6efcd deleted file mode 100644 index d317517..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/003f4abb35470011162d85db97d6efcd +++ /dev/null @@ -1,227 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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(); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/30a9784665460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/30a9784665460011162d85db97d6efcd deleted file mode 100644 index 0922e3f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/30a9784665460011162d85db97d6efcd +++ /dev/null @@ -1,7 +0,0 @@ -package binary_tree; - -public class BinaryTree { - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/30bb0fbd42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/30bb0fbd42490011162d85db97d6efcd deleted file mode 100644 index 60e4f0a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/30bb0fbd42490011162d85db97d6efcd +++ /dev/null @@ -1,367 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - Boolean flag; - - queueOfNodes.push(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - flag = flags.pop(); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/400a286a2f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/400a286a2f470011162d85db97d6efcd deleted file mode 100644 index 89e74bd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/400a286a2f470011162d85db97d6efcd +++ /dev/null @@ -1,129 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/f09a3bba81460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/f09a3bba81460011162d85db97d6efcd deleted file mode 100644 index 9e8f141..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/f09a3bba81460011162d85db97d6efcd +++ /dev/null @@ -1,43 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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(); - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ce/00b59c4a374d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/00b59c4a374d0011135ca7e1c9885acd new file mode 100644 index 0000000..045e11d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/00b59c4a374d0011135ca7e1c9885acd @@ -0,0 +1,107 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node penultimateNode = tail.prev; + + tail = penultimateNode; + penultimateNode.next = null; + lastNode.prev = null; + } + size--; + return tmp; + } + + public void addLast() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ce/801d225243490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/801d225243490011162d85db97d6efcd deleted file mode 100644 index 62b6fd0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ce/801d225243490011162d85db97d6efcd +++ /dev/null @@ -1,389 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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()); - flags.push(false); - } - - // visitiamo il nodo di sinistra infine - if (current.getLeft() != null) { - queueOfNodes.push(current.getRight()); - } - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ce/f01c00dd314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/f01c00dd314600111c1cd5b6f02d115c deleted file mode 100644 index 98771fb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ce/f01c00dd314600111c1cd5b6f02d115c +++ /dev/null @@ -1,33 +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; - } - - /** - * 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. - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cf/0023d91a36470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/0023d91a36470011162d85db97d6efcd deleted file mode 100644 index a552cd5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cf/0023d91a36470011162d85db97d6efcd +++ /dev/null @@ -1,231 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cf/301a058880460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/301a058880460011162d85db97d6efcd deleted file mode 100644 index 3f1ba9f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cf/301a058880460011162d85db97d6efcd +++ /dev/null @@ -1,27 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - // METODI - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cf/307e539c43490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/307e539c43490011162d85db97d6efcd deleted file mode 100644 index eb7e519..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cf/307e539c43490011162d85db97d6efcd +++ /dev/null @@ -1,396 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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()); - flags.push(false); - } - - // visitiamo il nodo di sinistra infine - if (current.getLeft() != null) { - queueOfNodes.push(current.getLeft()); - flags.push(false); - } - } - } - } - - public Iterator ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cf/40ca51a23b4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/40ca51a23b4600111c1cd5b6f02d115c deleted file mode 100644 index 485f82b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cf/40ca51a23b4600111c1cd5b6f02d115c +++ /dev/null @@ -1,60 +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 ordina l'array - */ - public void ordina() { - - } - - // FUNZIONI ASTRATTE - - /** - * Funzione che restituisce il valore della comparazione - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cf/608259318f4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/608259318f4c0011135ca7e1c9885acd new file mode 100644 index 0000000..2539a6a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/608259318f4c0011135ca7e1c9885acd @@ -0,0 +1,17 @@ +package jcf_map.exercise; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d/301721ca2e470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d/301721ca2e470011162d85db97d6efcd deleted file mode 100644 index c64616d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d/301721ca2e470011162d85db97d6efcd +++ /dev/null @@ -1,121 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - if (temp != null) { - temp.setData(null); - return true; - } - return false; - } - - public boolean contains(E targetElement) { - return find(targetElement, root) != null; - } - - // ITERATORI - - /* - * 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 - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d/50af1af25c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d/50af1af25c490011162d85db97d6efcd deleted file mode 100644 index b6184e9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d/50af1af25c490011162d85db97d6efcd +++ /dev/null @@ -1,469 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - - } - - public void printLeaf(BinaryNode 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()); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d/90afdba635470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d/90afdba635470011162d85db97d6efcd deleted file mode 100644 index 348b6e7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d/90afdba635470011162d85db97d6efcd +++ /dev/null @@ -1,225 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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()); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d/a0377873024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/d/a0377873024c00111d9cbe34e2b6027a deleted file mode 100644 index 6639914..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d/a0377873024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,5 +0,0 @@ -package jcf_set.exercise; - -public class IteratoreSenzaDuplicati { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d0/40962e082c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/40962e082c470011162d85db97d6efcd deleted file mode 100644 index 4aaf1ff..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d0/40962e082c470011162d85db97d6efcd +++ /dev/null @@ -1,93 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d0/608f4b8c314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/608f4b8c314600111c1cd5b6f02d115c deleted file mode 100644 index ddd90e0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d0/608f4b8c314600111c1cd5b6f02d115c +++ /dev/null @@ -1,25 +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; - } - - /** - * Funzione aggiungi - */ - public boolean aggiungi(Object elemento) { - if (elemento != null && dimensioneCorrente < dimensioneMassima) { - - } else return false; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d0/70e1a6a181460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/70e1a6a181460011162d85db97d6efcd deleted file mode 100644 index 8db3513..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d0/70e1a6a181460011162d85db97d6efcd +++ /dev/null @@ -1,41 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d0/a0deb2a4344d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/a0deb2a4344d0011135ca7e1c9885acd new file mode 100644 index 0000000..742c120 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/a0deb2a4344d0011135ca7e1c9885acd @@ -0,0 +1,80 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + head.next.prev = null; + + head = head.next; + + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/003c72e95d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/003c72e95d490011162d85db97d6efcd deleted file mode 100644 index 6fd5be6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/003c72e95d490011162d85db97d6efcd +++ /dev/null @@ -1,489 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 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 node, Integer number) { - if (node == null) return; - - if (node.getLeft() == null && node.getRight() == null) number++; - else { - numberLeaf(node.getLeft(), number); - numberLeaf(node.getRight(), number); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/10584cc97e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/10584cc97e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..d329ce2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/10584cc97e4c00111d9cbe34e2b6027a @@ -0,0 +1,50 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + for () { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/50ee9fac5b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/50ee9fac5b490011162d85db97d6efcd deleted file mode 100644 index fbd4536..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/50ee9fac5b490011162d85db97d6efcd +++ /dev/null @@ -1,448 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/604687a231470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/604687a231470011162d85db97d6efcd deleted file mode 100644 index 6781d78..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/604687a231470011162d85db97d6efcd +++ /dev/null @@ -1,182 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/a0ed897835470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/a0ed897835470011162d85db97d6efcd deleted file mode 100644 index 773cf41..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/a0ed897835470011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode current; - - queueOfNodes.add(node); - while(!queueOfNodes.isEmpty()) { - current = queueOfNodes.remove(); - - if (current.getData() != null) templist.add(current.getData()); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/e0008dce3e4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/e0008dce3e4600111c1cd5b6f02d115c deleted file mode 100644 index 9dd8e48..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/e0008dce3e4600111c1cd5b6f02d115c +++ /dev/null @@ -1,13 +0,0 @@ -package vettore_ordinabile; - -public class VettoriIntero { - - // Metodo costruttore default - /* - * Contiene solamente 10 elementi - * */ - public VettoriIntero() { - super(10); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/e0a8cfe23f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/e0a8cfe23f4600111c1cd5b6f02d115c deleted file mode 100644 index 62243ce..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/e0a8cfe23f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,54 +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 - */ - @Override - public boolean aggiungi(Object object) { - return false; - } - - // Metodo aggiungi - /* - * Questo metodo aggiunge realmente l'elemento di tipo - * Integer - */ - public boolean aggiungi(Integer integer) { - return super.aggiungi(integer); - } - - // Metodo ordina - /* - * Questo metodo permette il funzionamento dell'ordinamento - * tramite comparatore simulato - */ - @Override - public int ordina(Object elemento1, Object elemento2) { - Integer i1 = (Integer)elemento1; - Integer i2 = (Integer)elemento2; - - if (i1 < i2) return -1; - if (i1 > i2) return 1; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/f0cf075733470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/f0cf075733470011162d85db97d6efcd deleted file mode 100644 index d15cdcb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/f0cf075733470011162d85db97d6efcd +++ /dev/null @@ -1,210 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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) - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/00c04c0a3a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/00c04c0a3a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..ad40349 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/00c04c0a3a4d0011135ca7e1c9885acd @@ -0,0 +1,137 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/3017d76a384600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/3017d76a384600111c1cd5b6f02d115c deleted file mode 100644 index 7d04d7f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/3017d76a384600111c1cd5b6f02d115c +++ /dev/null @@ -1,49 +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; - } - - /** - * 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 ordina l'array - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/5021b4c4394d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/5021b4c4394d0011135ca7e1c9885acd new file mode 100644 index 0000000..267ced7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/5021b4c4394d0011135ca7e1c9885acd @@ -0,0 +1,134 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/5034f47c2f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/5034f47c2f470011162d85db97d6efcd deleted file mode 100644 index 25ba616..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/5034f47c2f470011162d85db97d6efcd +++ /dev/null @@ -1,131 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft()); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/70d31a4d7e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/70d31a4d7e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..3e4d457 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/70d31a4d7e4c00111d9cbe34e2b6027a @@ -0,0 +1,45 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/90ac7c6137470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/90ac7c6137470011162d85db97d6efcd deleted file mode 100644 index b25e943..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/90ac7c6137470011162d85db97d6efcd +++ /dev/null @@ -1,232 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/c0d09a6076460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/c0d09a6076460011162d85db97d6efcd deleted file mode 100644 index ea08abb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/c0d09a6076460011162d85db97d6efcd +++ /dev/null @@ -1,135 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - - while (cur.parent != null) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d3/90b2d2595e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d3/90b2d2595e490011162d85db97d6efcd deleted file mode 100644 index be5ace7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d3/90b2d2595e490011162d85db97d6efcd +++ /dev/null @@ -1,487 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 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 node) { - if (node == null) return 0; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d4/002684402f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/d4/002684402f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..82a59cf --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d4/002684402f4d0011135ca7e1c9885acd @@ -0,0 +1,36 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node tail = null; + private int size = 0; + + // Costruttore + public MyLinkedList() {} + + // Metodi +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d4/0069eab5024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/d4/0069eab5024c00111d9cbe34e2b6027a deleted file mode 100644 index 39d270e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d4/0069eab5024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,17 +0,0 @@ -package jcf_set.exercise; - -import java.util.List; - -public class IteratoreSenzaDuplicati { - - public static void main(String[] main) { - new IteratoreSenzaDuplicati().run(); - } - - public void run() { - - List lista = new ArrayList(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d4/c048208931470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d4/c048208931470011162d85db97d6efcd deleted file mode 100644 index 415b187..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d4/c048208931470011162d85db97d6efcd +++ /dev/null @@ -1,180 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d4/c0b387c6314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/d4/c0b387c6314600111c1cd5b6f02d115c deleted file mode 100644 index 44949b1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d4/c0b387c6314600111c1cd5b6f02d115c +++ /dev/null @@ -1,31 +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; - } - - /** - * Funzione aggiungi - */ - public boolean aggiungi(Object elemento) { - if (elemento != null && dimensioneCorrente < dimensioneMassima) { - vettore[dimensioneCorrente] = elemento; - dimensioneCorrente++; - return true; - } else return false; - } - - /** - * - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d4/e01abfb742490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d4/e01abfb742490011162d85db97d6efcd deleted file mode 100644 index 1929532..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d4/e01abfb742490011162d85db97d6efcd +++ /dev/null @@ -1,366 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - Boolean flag; - - queueOfNodes.push(node); - while (!queueOfNodes.isEmpty()) { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d4/f06d713e034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/d4/f06d713e034c00111d9cbe34e2b6027a deleted file mode 100644 index 481d012..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d4/f06d713e034c00111d9cbe34e2b6027a +++ /dev/null @@ -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 lista = new ArrayList(); - - 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 Iterator getIteratorNoDuplicates(Iterator it) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d5/00090f0577460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d5/00090f0577460011162d85db97d6efcd deleted file mode 100644 index 5486b71..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d5/00090f0577460011162d85db97d6efcd +++ /dev/null @@ -1,149 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - - while (cur.parent != null){ - d++; - cur = cur.parent; - } - - return d; - } - - public int height() { - if (left == null && right == null) return 0; - - int hLeft = (left == null) ? 0 : left.height(); - int hRight = (right == null) ? 0 : right.height(); - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d5/404e44ab324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/d5/404e44ab324d0011135ca7e1c9885acd new file mode 100644 index 0000000..2b3dc54 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d5/404e44ab324d0011135ca7e1c9885acd @@ -0,0 +1,72 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d5/805269c682460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d5/805269c682460011162d85db97d6efcd deleted file mode 100644 index 9b8c053..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d5/805269c682460011162d85db97d6efcd +++ /dev/null @@ -1,57 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode node) { - if (node.getLeft() == null && node.getRight() == null) return 0; - - int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d5/80ce7fe5024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/d5/80ce7fe5024c00111d9cbe34e2b6027a deleted file mode 100644 index 6d11632..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d5/80ce7fe5024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,26 +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 lista = new ArrayList(); - - lista.add("tree"); - lista.add("flower"); - lista.add("tree"); - lista.add("flower"); - lista.add("animal"); - lista.add("flower"); - lista.add("fruits"); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d6/9004a26f6b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d6/9004a26f6b460011162d85db97d6efcd deleted file mode 100644 index 90cea23..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d6/9004a26f6b460011162d85db97d6efcd +++ /dev/null @@ -1,106 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode oldParent = parent; - parent = null; - - if (oldParent.left == this) oldParent.left = null; - else oldParent.right = null; - - return oldParent; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d7/30fc1da4354d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/d7/30fc1da4354d0011135ca7e1c9885acd new file mode 100644 index 0000000..e37e599 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d7/30fc1da4354d0011135ca7e1c9885acd @@ -0,0 +1,86 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d7/d031aeb59b4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/d7/d031aeb59b4c0011135ca7e1c9885acd new file mode 100644 index 0000000..9c5bd49 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d7/d031aeb59b4c0011135ca7e1c9885acd @@ -0,0 +1,30 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + if (myList.isEmpty()) return false; + // Verifica che la lista si pari o dispari + HashMap valori = new HashMap(); + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d7/d06ae78c7f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/d7/d06ae78c7f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..4768222 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d7/d06ae78c7f4c00111d9cbe34e2b6027a @@ -0,0 +1,58 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d7/e0cdf0f77e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/d7/e0cdf0f77e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..8693624 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d7/e0cdf0f77e4c00111d9cbe34e2b6027a @@ -0,0 +1,51 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + for (String s : archivio.keySet()) { + Cliente currentCliente = archivio.get(s); + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d8/609026e733490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d8/609026e733490011162d85db97d6efcd deleted file mode 100644 index 98764b5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d8/609026e733490011162d85db97d6efcd +++ /dev/null @@ -1,270 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 il primo 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - if (current) - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d8/80ed6fd52f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/d8/80ed6fd52f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..06ea5d1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d8/80ed6fd52f4d0011135ca7e1c9885acd @@ -0,0 +1,54 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d9/10f6bd5932490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/10f6bd5932490011162d85db97d6efcd deleted file mode 100644 index 96a17f9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d9/10f6bd5932490011162d85db97d6efcd +++ /dev/null @@ -1,246 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator iterator() { - return iteratorPreOrder(); - } - - // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) - - protected void itpreorder() { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode current; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d9/7099efa73c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/7099efa73c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..d666748 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/7099efa73c4d0011135ca7e1c9885acd @@ -0,0 +1,173 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + return currentNode.data; + } + + public E set(int index, E item) { + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d9/808f61117f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/808f61117f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..12f46e6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/808f61117f4c00111d9cbe34e2b6027a @@ -0,0 +1,52 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + for (String s : archivio.keySet()) { + Cliente currentCliente = archivio.get(s); + + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d9/e02ca87a3c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/e02ca87a3c4600111c1cd5b6f02d115c deleted file mode 100644 index e5bda90..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d9/e02ca87a3c4600111c1cd5b6f02d115c +++ /dev/null @@ -1,77 +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; - for (int j = i+1; j < dimensioneCorrente; j++) { - if (confronta(vettore[minimo], vettore[j]) > 0) minimo = j; - if (vettore[minimo] > vettore[j]) minimo = j; - } - - } - } - - // FUNZIONI ASTRATTE - - /** - * Funzione che restituisce il valore della comparazione - */ - protected abstract int confronta(Object elemento1, Object elemento2); -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d9/e0b72a69a84c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/e0b72a69a84c0011135ca7e1c9885acd new file mode 100644 index 0000000..21e3c23 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/e0b72a69a84c0011135ca7e1c9885acd @@ -0,0 +1,7 @@ +package jcf_map.exercise; + +public class BinaryTreeMap { + /* + * Esercizio 1 - Esame del secondo parziale 8/1/2019 + */ +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/da/10811de0a94c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/da/10811de0a94c0011135ca7e1c9885acd new file mode 100644 index 0000000..f08fff1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/da/10811de0a94c0011135ca7e1c9885acd @@ -0,0 +1,18 @@ +package jcf_map.exercise; + +import java.util.Map; + +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 Map ContaDuplicati(BinaryNode node) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/da/908b1c2eaa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/da/908b1c2eaa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..442fcab --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/da/908b1c2eaa4c0011135ca7e1c9885acd @@ -0,0 +1,24 @@ +package jcf_map.exercise; + +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/da/c017ad9e374d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/da/c017ad9e374d0011135ca7e1c9885acd new file mode 100644 index 0000000..3fb13dd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/da/c017ad9e374d0011135ca7e1c9885acd @@ -0,0 +1,114 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = new Node(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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(); + if (size == 0) { + head = tail = new Node(item, null, null); + } else { + Node newNode = new Node(); + } + size++; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/db/70e01b3038470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/db/70e01b3038470011162d85db97d6efcd deleted file mode 100644 index f82d42a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/db/70e01b3038470011162d85db97d6efcd +++ /dev/null @@ -1,239 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator iterator() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/db/a0d06ad730470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/db/a0d06ad730470011162d85db97d6efcd deleted file mode 100644 index d6cfe42..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/db/a0d06ad730470011162d85db97d6efcd +++ /dev/null @@ -1,165 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - inorder(root, temporaryList); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/db/f06f88bdab4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/db/f06f88bdab4c0011135ca7e1c9885acd new file mode 100644 index 0000000..c2e5da7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/db/f06f88bdab4c0011135ca7e1c9885acd @@ -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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, TreeMap 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(data, 1); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/0096812236470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/0096812236470011162d85db97d6efcd deleted file mode 100644 index 90c2a41..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/0096812236470011162d85db97d6efcd +++ /dev/null @@ -1,231 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/00d0ae521b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/00d0ae521b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..da3430a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/00d0ae521b4d0011135ca7e1c9885acd @@ -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 + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/206722a634470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/206722a634470011162d85db97d6efcd deleted file mode 100644 index 6b02ec6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/206722a634470011162d85db97d6efcd +++ /dev/null @@ -1,214 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/3055a4825d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/3055a4825d490011162d85db97d6efcd deleted file mode 100644 index f081272..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/3055a4825d490011162d85db97d6efcd +++ /dev/null @@ -1,484 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public void numberLeaf() { - Integer number = 0; - numberLeaf(root, number); - } - - public void numberLeaf(BinaryNode node, Integer number) { - if (node == null) return; - - if () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/309b6145084c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/309b6145084c00111d9cbe34e2b6027a deleted file mode 100644 index aa5dc92..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/309b6145084c00111d9cbe34e2b6027a +++ /dev/null @@ -1,5 +0,0 @@ -package jcf_set.exercise; - -public class Insiemistica { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/400537d3024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/400537d3024c00111d9cbe34e2b6027a deleted file mode 100644 index 52b823a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/400537d3024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,21 +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 lista = new ArrayList(); - - lista.add("tree"); - lista.add(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/50fce311374d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/50fce311374d0011135ca7e1c9885acd new file mode 100644 index 0000000..bf9af33 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/50fce311374d0011135ca7e1c9885acd @@ -0,0 +1,101 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node penultimateNode = tail.prev; + + tail = penultimateNode; + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/6071be1435470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/6071be1435470011162d85db97d6efcd deleted file mode 100644 index 817d951..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/6071be1435470011162d85db97d6efcd +++ /dev/null @@ -1,223 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode current; - - queueOfNodes.add(node); - while(!queueOfNodes.isEmpty()) { - current = queueOfNodes.remove(); - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/90fdd8db7d4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/90fdd8db7d4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..be678ed --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/90fdd8db7d4c00111d9cbe34e2b6027a @@ -0,0 +1,22 @@ +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) { + this.nominativo = nominativo; + this.cf = cf; + this.cittaResidenza = cittaResidenza; + } + + // Getters + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/80872a1a9c4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/80872a1a9c4c0011135ca7e1c9885acd new file mode 100644 index 0000000..b126951 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/80872a1a9c4c0011135ca7e1c9885acd @@ -0,0 +1,39 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + if (myList.isEmpty()) return true; + // Verifica che la lista si pari o dispari + HashMap valori = new HashMap(); + + Iterator 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); + } + + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/90827eac9b4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/90827eac9b4c0011135ca7e1c9885acd new file mode 100644 index 0000000..f5379b7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/90827eac9b4c0011135ca7e1c9885acd @@ -0,0 +1,29 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Verifica che la lista si pari o dispari + HashMap valori = new HashMap(); + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/b0ef216d37470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/b0ef216d37470011162d85db97d6efcd deleted file mode 100644 index 9a2c881..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/b0ef216d37470011162d85db97d6efcd +++ /dev/null @@ -1,233 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/c0c4e1405d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/c0c4e1405d490011162d85db97d6efcd deleted file mode 100644 index f948d36..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/c0c4e1405d490011162d85db97d6efcd +++ /dev/null @@ -1,481 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public void numberLeaf() { - - } - - public void printLeaf() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/d0b636b4084c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/d0b636b4084c00111d9cbe34e2b6027a deleted file mode 100644 index bd566ce..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/d0b636b4084c00111d9cbe34e2b6027a +++ /dev/null @@ -1,16 +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}; - Integer[] bArray = {1, 5, -6, -4, 5}; - - // Input come HashSet (Ordine non garantito) - Set aHashSet = new HashSet(aArray); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/e050b6832d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/e050b6832d470011162d85db97d6efcd deleted file mode 100644 index f596f82..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/e050b6832d470011162d85db97d6efcd +++ /dev/null @@ -1,111 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - if (temp != null) { - temp.setData(null); - return true; - } - return false; - } - - public boolean contains(E targetElement) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/de/50ba347e42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/de/50ba347e42490011162d85db97d6efcd deleted file mode 100644 index 6c59f84..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/de/50ba347e42490011162d85db97d6efcd +++ /dev/null @@ -1,358 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/de/907414763a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/de/907414763a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..195bbc6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/de/907414763a4d0011135ca7e1c9885acd @@ -0,0 +1,145 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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; + } + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/de/e08d3fb6024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/de/e08d3fb6024c00111d9cbe34e2b6027a deleted file mode 100644 index 4ff379c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/de/e08d3fb6024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,18 +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 lista = new ArrayList(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/de/e0f7a0da2f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/de/e0f7a0da2f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..778b6be --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/de/e0f7a0da2f4d0011135ca7e1c9885acd @@ -0,0 +1,55 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/df/0090896c084c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/df/0090896c084c00111d9cbe34e2b6027a deleted file mode 100644 index 5b72030..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/df/0090896c084c00111d9cbe34e2b6027a +++ /dev/null @@ -1,13 +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}; - Integer[] bArray = {1, 5, -6, -4, 5}; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/df/109d6d1b81460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/df/109d6d1b81460011162d85db97d6efcd deleted file mode 100644 index 06777b3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/df/109d6d1b81460011162d85db97d6efcd +++ /dev/null @@ -1,40 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(left.root, data, right.root); - size = 1 + left.size + right.size; - } - - // METODI - - public int size() { - return size; - } - - public boolean isEmpty() { - return (size == 0); - } - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/df/10a1dbe03f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/df/10a1dbe03f4600111c1cd5b6f02d115c deleted file mode 100644 index 8485ba1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/df/10a1dbe03f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,53 +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 - */ - @Override - public boolean aggiungi(Object object) { - return false; - } - - // Metodo aggiungi - /* - * Questo metodo aggiunge realmente l'elemento di tipo - * Integer - */ - public boolean aggiungi(Integer integer) { - return super.aggiungi(integer); - } - - // Metodo ordina - /* - * Questo metodo permette il funzionamento dell'ordinamento - * tramite comparatore simulato - */ - @Override - public int ordina(Object elemento1, Object elemento2) { - Integer i1 = (Integer)elemento1; - Integer i2 = (Integer)elemento2; - - if (i1 < i2) return -1; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/df/10c313473c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/df/10c313473c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..91c7ed3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/df/10c313473c4d0011135ca7e1c9885acd @@ -0,0 +1,164 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/df/400e6765aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/df/400e6765aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..5d942c8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/df/400e6765aa4c0011135ca7e1c9885acd @@ -0,0 +1,30 @@ +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + if (node == null) return mappa; + + if (node.getLeft() != null) ContaDuplicati(node.getLeft(), mappa); + if (node.getRight() != null) ContaDuplicati(node.getRight(), mappa); + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/df/d0665ef5084c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/df/d0665ef5084c00111d9cbe34e2b6027a deleted file mode 100644 index 5419ade..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/df/d0665ef5084c00111d9cbe34e2b6027a +++ /dev/null @@ -1,23 +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 aHashSet = new HashSet(Arrays.asList(aArray)); - Set bHashSet = new HashSet(Arrays.asList(bArray)); - - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e/301fc98031470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e/301fc98031470011162d85db97d6efcd deleted file mode 100644 index 2a2b13a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e/301fc98031470011162d85db97d6efcd +++ /dev/null @@ -1,178 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 - */ - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e/e080bf633b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e/e080bf633b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..cb7555d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e/e080bf633b4d0011135ca7e1c9885acd @@ -0,0 +1,150 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e/e098237e9b4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e/e098237e9b4c0011135ca7e1c9885acd new file mode 100644 index 0000000..ef3f833 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e/e098237e9b4c0011135ca7e1c9885acd @@ -0,0 +1,27 @@ +package jcf_map.exercise; + +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Verifica che la lista si pari o dispari + HashMap valori = new HashMap(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e/f01b0ddc384d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e/f01b0ddc384d0011135ca7e1c9885acd new file mode 100644 index 0000000..5c70255 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e/f01b0ddc384d0011135ca7e1c9885acd @@ -0,0 +1,126 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node oldLastNode = tail; + oldLastNode.next = newNode; + newNode.prev = oldLastNode; + tail = newNode; + } + size++; + } + + public void clear() { + head = tail = null; + size = 0; + } + + public boolean add() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e/f0675cc2344d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e/f0675cc2344d0011135ca7e1c9885acd new file mode 100644 index 0000000..d0d99b0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e/f0675cc2344d0011135ca7e1c9885acd @@ -0,0 +1,81 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node secondPlace = head.next; + head.next.prev = null; + + head = head.next; + + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e0/40f1d7103c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/40f1d7103c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..25990d1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/40f1d7103c4d0011135ca7e1c9885acd @@ -0,0 +1,163 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e0/7041a0625a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/7041a0625a490011162d85db97d6efcd deleted file mode 100644 index 0397ece..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e0/7041a0625a490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - E currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - currentNode.setData(currentObject); - - if (wing == 0) { - // Left Wing - - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e0/808f90b0374d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/808f90b0374d0011135ca7e1c9885acd new file mode 100644 index 0000000..efd0825 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/808f90b0374d0011135ca7e1c9885acd @@ -0,0 +1,113 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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(); + if (size == 0) { + head = tail = new Node(item, null, null); + } else { + Node newNode = new Node(); + } + size++; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e0/9034c3df314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/9034c3df314600111c1cd5b6f02d115c deleted file mode 100644 index a010378..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e0/9034c3df314600111c1cd5b6f02d115c +++ /dev/null @@ -1,36 +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; - } - - /** - * 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) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e0/908afa513e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/908afa513e490011162d85db97d6efcd deleted file mode 100644 index 7ddc2bf..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e0/908afa513e490011162d85db97d6efcd +++ /dev/null @@ -1,296 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e0/c047f200ac4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/c047f200ac4c0011135ca7e1c9885acd new file mode 100644 index 0000000..de5cc3e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/c047f200ac4c0011135ca7e1c9885acd @@ -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 Map ContaDuplicati(BinaryNode node) { + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + return mappa; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e0/c0b110f442490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/c0b110f442490011162d85db97d6efcd deleted file mode 100644 index 56dfe93..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e0/c0b110f442490011162d85db97d6efcd +++ /dev/null @@ -1,374 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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(); - } else { - // il nodo non è ancora da visitare - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e0/e0a1ab9e1b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/e0a1ab9e1b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..a7e6536 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/e0a1ab9e1b4d0011135ca7e1c9885acd @@ -0,0 +1,36 @@ +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 + */ + + public void run() { + TreeMap mappaAssociazione = new TreeMap(); + + companyMap.put("Rossi Marco", 8); + companyMap.put("Bianchi Luca", 14); + companyMap.put("Esposito Andrea", 6); + companyMap.put("Ferrari Matteo", 6); + companyMap.put("Romano Giulia", 14); + companyMap.put("Ricci Alessia", 6); + + System.out.println("Stampa mappa associazione: "); + System.out.println(mappaAssociazione); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e1/208feab2054c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/e1/208feab2054c00111d9cbe34e2b6027a deleted file mode 100644 index e69de29..0000000 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e1/302c0c891e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e1/302c0c891e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..4693970 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e1/302c0c891e4d0011135ca7e1c9885acd @@ -0,0 +1,27 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Data + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e1/605e520b43490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e1/605e520b43490011162d85db97d6efcd deleted file mode 100644 index 2afbc9f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e1/605e520b43490011162d85db97d6efcd +++ /dev/null @@ -1,375 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 - queueOfNodes.push(current); - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e1/80b123895d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e1/80b123895d490011162d85db97d6efcd deleted file mode 100644 index db38dcc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e1/80b123895d490011162d85db97d6efcd +++ /dev/null @@ -1,484 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public void numberLeaf() { - Integer number = 0; - numberLeaf(root, number); - } - - public void numberLeaf(BinaryNode node, Integer number) { - if (node == null) return; - - if (node.getLeft() == null && node.getRight() == null) number++; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e2/1080eb132c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/1080eb132c470011162d85db97d6efcd deleted file mode 100644 index 234004d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e2/1080eb132c470011162d85db97d6efcd +++ /dev/null @@ -1,93 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e2/30d2b8e69b4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/30d2b8e69b4c0011135ca7e1c9885acd new file mode 100644 index 0000000..470f74d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/30d2b8e69b4c0011135ca7e1c9885acd @@ -0,0 +1,35 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + if (myList.isEmpty()) return true; + // Verifica che la lista si pari o dispari + HashMap valori = new HashMap(); + + Iterator it = myList.iterator(); + while (it.hasNext()) { + + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e2/30d742b77d4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/30d742b77d4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..fe070b0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/30d742b77d4c00111d9cbe34e2b6027a @@ -0,0 +1,15 @@ +package parziale.p191108; + +public class Cliente { + + // Variabili di instanza + private String nominativo; + private String cf; + private String cittaResidenza; + + // Costruttore + public Cliente( + ) { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e2/608fe2f580460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/608fe2f580460011162d85db97d6efcd deleted file mode 100644 index 2ffc5a7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e2/608fe2f580460011162d85db97d6efcd +++ /dev/null @@ -1,32 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(left.root, data, right.root); - size = 1; - } - - // METODI - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e2/703bdb1243490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/703bdb1243490011162d85db97d6efcd deleted file mode 100644 index 9608d85..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e2/703bdb1243490011162d85db97d6efcd +++ /dev/null @@ -1,376 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 - queueOfNodes.push(current); - flags.push(true); - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e2/d0b3cf44aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/d0b3cf44aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..f1fa49e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/d0b3cf44aa4c0011135ca7e1c9885acd @@ -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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + if (node == null) return mappa; + + if (node.getLeft() != null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e3/4041b3c82c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/4041b3c82c470011162d85db97d6efcd deleted file mode 100644 index bd28c99..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e3/4041b3c82c470011162d85db97d6efcd +++ /dev/null @@ -1,101 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode resNode; - resNode = find(targetElement, root.getLeft()); - if (resNode == null) resNode = find(targetElement, root.getRight()); - return resNode; - } - - public boolean remove(E targetElement) { - if (targetElement == null) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e3/5042242d7d4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/5042242d7d4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..91afe96 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/5042242d7d4c00111d9cbe34e2b6027a @@ -0,0 +1,41 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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. + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e3/609cff06304d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/609cff06304d0011135ca7e1c9885acd new file mode 100644 index 0000000..65b5a6b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/609cff06304d0011135ca7e1c9885acd @@ -0,0 +1,57 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e3/900ac8d334470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/900ac8d334470011162d85db97d6efcd deleted file mode 100644 index feedbf9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e3/900ac8d334470011162d85db97d6efcd +++ /dev/null @@ -1,220 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - - queueOfNodes.add(node); - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e3/e06c01635d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/e06c01635d490011162d85db97d6efcd deleted file mode 100644 index d33d8ef..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e3/e06c01635d490011162d85db97d6efcd +++ /dev/null @@ -1,482 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - printLeaf(root); - } - - public void printLeaf(BinaryNode 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 un metodo che conta il numero - * di foglie dell'albero corrente - */ - public void numberLeaf() { - Integer number = 0; - numberLeaf(root, number); - } - - public void numberLeaf(BinaryNode node) { - if () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e4/10a985f37f460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e4/10a985f37f460011162d85db97d6efcd deleted file mode 100644 index 78e253f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e4/10a985f37f460011162d85db97d6efcd +++ /dev/null @@ -1,16 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - - - - // METODI - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e4/205450e8354d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e4/205450e8354d0011135ca7e1c9885acd new file mode 100644 index 0000000..aecb996 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e4/205450e8354d0011135ca7e1c9885acd @@ -0,0 +1,91 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e4/4087a6a067460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e4/4087a6a067460011162d85db97d6efcd deleted file mode 100644 index d4f9594..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e4/4087a6a067460011162d85db97d6efcd +++ /dev/null @@ -1,33 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode right) { - this.data = data; - - this.left = left; - this.right = right; - - this.parent = null; - - - } - - // METODI - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e4/60912d3b41490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e4/60912d3b41490011162d85db97d6efcd deleted file mode 100644 index 79c83c5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e4/60912d3b41490011162d85db97d6efcd +++ /dev/null @@ -1,336 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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); - } - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e4/9030d0709b4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e4/9030d0709b4c0011135ca7e1c9885acd new file mode 100644 index 0000000..364e93c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e4/9030d0709b4c0011135ca7e1c9885acd @@ -0,0 +1,27 @@ +package jcf_map.exercise; + +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Verifica che la lista si pari o dispari + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e4/b005ce578f4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e4/b005ce578f4c0011135ca7e1c9885acd new file mode 100644 index 0000000..1075a65 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e4/b005ce578f4c0011135ca7e1c9885acd @@ -0,0 +1,20 @@ +package jcf_map.exercise; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e5/c05a5ccf2e470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e5/c05a5ccf2e470011162d85db97d6efcd deleted file mode 100644 index b155bf7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e5/c05a5ccf2e470011162d85db97d6efcd +++ /dev/null @@ -1,123 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - if (temp != null) { - temp.setData(null); - return true; - } - return false; - } - - public boolean contains(E targetElement) { - return find(targetElement, root) != null; - } - - // ITERATORI - - /* - * 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 - */ - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e6/40a645103f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/e6/40a645103f4600111c1cd5b6f02d115c deleted file mode 100644 index 8c8ee80..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e6/40a645103f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,23 +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 - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e6/501f93412c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e6/501f93412c470011162d85db97d6efcd deleted file mode 100644 index 587a26e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e6/501f93412c470011162d85db97d6efcd +++ /dev/null @@ -1,95 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode resNode; - resNode = find(targetElement, root.getLeft()); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e6/a0c5a868034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/e6/a0c5a868034c00111d9cbe34e2b6027a deleted file mode 100644 index cc9858f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e6/a0c5a868034c00111d9cbe34e2b6027a +++ /dev/null @@ -1,40 +0,0 @@ -package jcf_set.exercise; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; - -public class IteratoreSenzaDuplicati { - - public static void main(String[] main) { - new IteratoreSenzaDuplicati().run(); - } - - public void run() { - - List lista = new ArrayList(); - - 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 Iterator getIteratorNoDuplicates(Iterator it) { - HashSet tmp = new HashSet(); - while (it.hasNext()) tmp.add(it.next()); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e7/3013d1131f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/3013d1131f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..1ff4cbc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/3013d1131f4d0011135ca7e1c9885acd @@ -0,0 +1,31 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node tail = null; + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e7/402179e37f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/402179e37f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..b9073de --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/402179e37f4c00111d9cbe34e2b6027a @@ -0,0 +1,68 @@ +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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(new Comparator() { + @Override + public int compare(String s1, String s2) { + return s1.compareTo(s2); + } + }); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e7/80bc1cef36470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/80bc1cef36470011162d85db97d6efcd deleted file mode 100644 index 8611438..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e7/80bc1cef36470011162d85db97d6efcd +++ /dev/null @@ -1,232 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e7/a03b88fb40490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/a03b88fb40490011162d85db97d6efcd deleted file mode 100644 index d494fea..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e7/a03b88fb40490011162d85db97d6efcd +++ /dev/null @@ -1,332 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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(); - } - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e7/f00b77c673460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/f00b77c673460011162d85db97d6efcd deleted file mode 100644 index 6f44d5c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e7/f00b77c673460011162d85db97d6efcd +++ /dev/null @@ -1,119 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e8/5007538483460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e8/5007538483460011162d85db97d6efcd deleted file mode 100644 index 70386b0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e8/5007538483460011162d85db97d6efcd +++ /dev/null @@ -1,60 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode node) { - if (node.getLeft() == null && node.getRight() == 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; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e8/8076a4e36a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e8/8076a4e36a460011162d85db97d6efcd deleted file mode 100644 index 608b332..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e8/8076a4e36a460011162d85db97d6efcd +++ /dev/null @@ -1,98 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public setAsRoot() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/0020d68f42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/0020d68f42490011162d85db97d6efcd deleted file mode 100644 index f8d9960..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/0020d68f42490011162d85db97d6efcd +++ /dev/null @@ -1,363 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - Boolean flag; - - queueOfNodes.add(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/307aa9f542490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/307aa9f542490011162d85db97d6efcd deleted file mode 100644 index 3e27413..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/307aa9f542490011162d85db97d6efcd +++ /dev/null @@ -1,374 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/5026957837470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/5026957837470011162d85db97d6efcd deleted file mode 100644 index 55e2fe0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/5026957837470011162d85db97d6efcd +++ /dev/null @@ -1,235 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/50ef8c9b42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/50ef8c9b42490011162d85db97d6efcd deleted file mode 100644 index c0126fe..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/50ef8c9b42490011162d85db97d6efcd +++ /dev/null @@ -1,363 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - Boolean flag; - - queueOfNodes.push(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/600eab47aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/600eab47aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..0425517 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/600eab47aa4c0011135ca7e1c9885acd @@ -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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + if (node == null) return mappa; + + if (node.getLeft() != null) ContaDuplicati(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/a02e45213a4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/a02e45213a4d0011135ca7e1c9885acd new file mode 100644 index 0000000..d6cfed4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/a02e45213a4d0011135ca7e1c9885acd @@ -0,0 +1,140 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/b008311a034c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/b008311a034c00111d9cbe34e2b6027a deleted file mode 100644 index 452e1af..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/b008311a034c00111d9cbe34e2b6027a +++ /dev/null @@ -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 lista = new ArrayList(); - - 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 Iterator - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/c0bc73d63b4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/c0bc73d63b4600111c1cd5b6f02d115c deleted file mode 100644 index 063eff5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/c0bc73d63b4600111c1cd5b6f02d115c +++ /dev/null @@ -1,65 +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 ordina l'array - */ - public void ordina() { - for (int i = 0; i < dimensioneCorrente; i++) { - int minimo = i; - for (int j = i+1; j < dimensioneCorrente; j++) { - - } - } - } - - // FUNZIONI ASTRATTE - - /** - * Funzione che restituisce il valore della comparazione - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/008f350674460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/008f350674460011162d85db97d6efcd deleted file mode 100644 index e15e97c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/008f350674460011162d85db97d6efcd +++ /dev/null @@ -1,124 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/00d8648259490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/00d8648259490011162d85db97d6efcd deleted file mode 100644 index c037bb4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/00d8648259490011162d85db97d6efcd +++ /dev/null @@ -1,425 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/10cbff122d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/10cbff122d470011162d85db97d6efcd deleted file mode 100644 index 76730c3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/10cbff122d470011162d85db97d6efcd +++ /dev/null @@ -1,106 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - if (temp != null) { - temp.setData(null); - } - return false; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/c07bc3dc76460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/c07bc3dc76460011162d85db97d6efcd deleted file mode 100644 index 060b8a1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/c07bc3dc76460011162d85db97d6efcd +++ /dev/null @@ -1,146 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - - while (cur.parent != null){ - d++; - cur = cur.parent; - } - - return d; - } - - public int height() { - if (left == null && right == null) return 0; - - int hLeft = (left == null) ? 0 : left.height(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/20c46eea314d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/20c46eea314d0011135ca7e1c9885acd new file mode 100644 index 0000000..52d298e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/20c46eea314d0011135ca7e1c9885acd @@ -0,0 +1,62 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + newNode.next = head; + head = newNode; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/507c0e3f374d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/507c0e3f374d0011135ca7e1c9885acd new file mode 100644 index 0000000..315a97a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/507c0e3f374d0011135ca7e1c9885acd @@ -0,0 +1,103 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node penultimateNode = tail.prev; + + tail = penultimateNode; + penultimateNode.next = null; + lastNode.prev = null; + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/60f3acd676460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/60f3acd676460011162d85db97d6efcd deleted file mode 100644 index d417c4f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/60f3acd676460011162d85db97d6efcd +++ /dev/null @@ -1,146 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - - while (cur.parent != null){ - d++; - cur = cur.parent; - } - - return d; - } - - public int height() { - if (left == null && right == null) return 0; - - int hLeft = (left == null) ? 0 : height(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/70e27a56374d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/70e27a56374d0011135ca7e1c9885acd new file mode 100644 index 0000000..dcda986 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/70e27a56374d0011135ca7e1c9885acd @@ -0,0 +1,107 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node penultimateNode = tail.prev; + + tail = penultimateNode; + penultimateNode.next = null; + lastNode.prev = null; + } + size--; + return tmp; + } + + public void addLast(E item) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/a07c186e354d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/a07c186e354d0011135ca7e1c9885acd new file mode 100644 index 0000000..b07c0ca --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/a07c186e354d0011135ca7e1c9885acd @@ -0,0 +1,85 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node secondPlace = head.next; + + head = secondPlace; + secondPlace.prev = null; + firstPlace.next = null; + } + size--; + return tmp; + } + + public E getLast() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/c00f466276460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/c00f466276460011162d85db97d6efcd deleted file mode 100644 index e1d1dbd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/c00f466276460011162d85db97d6efcd +++ /dev/null @@ -1,137 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - - while (cur.parent != null){ - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/c0b4ba7e35470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/c0b4ba7e35470011162d85db97d6efcd deleted file mode 100644 index febee55..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/c0b4ba7e35470011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode current; - - queueOfNodes.add(node); - while(!queueOfNodes.isEmpty()) { - current = queueOfNodes.remove(); - - if (current.getData() != null) temporaryList.add(current.getData()); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/30c7589d384600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/30c7589d384600111c1cd5b6f02d115c deleted file mode 100644 index fc3085c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/30c7589d384600111c1cd5b6f02d115c +++ /dev/null @@ -1,58 +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 ordina l'array - */ - public void ordina() { - - } - - // FUNZIONI ASTRATTE - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/40e60d222e470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/40e60d222e470011162d85db97d6efcd deleted file mode 100644 index 91bb173..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/40e60d222e470011162d85db97d6efcd +++ /dev/null @@ -1,113 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - if (temp != null) { - temp.setData(null); - return true; - } - return false; - } - - public boolean contains(E targetElement) { - return find(targetElement, root) != null; - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/60662fc75b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/60662fc75b490011162d85db97d6efcd deleted file mode 100644 index 0e087ee..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/60662fc75b490011162d85db97d6efcd +++ /dev/null @@ -1,449 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/70c3454f41490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/70c3454f41490011162d85db97d6efcd deleted file mode 100644 index d8dcac9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/70c3454f41490011162d85db97d6efcd +++ /dev/null @@ -1,340 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/00c5c70e334d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/00c5c70e334d0011135ca7e1c9885acd new file mode 100644 index 0000000..01a4da3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/00c5c70e334d0011135ca7e1c9885acd @@ -0,0 +1,76 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 { + + } + size--; + return tmp; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/80f11f062f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/20fd17937a4c00111d9cbe34e2b6027a similarity index 100% rename from .metadata/.plugins/org.eclipse.core.resources/.history/bf/80f11f062f4600111c1cd5b6f02d115c rename to .metadata/.plugins/org.eclipse.core.resources/.history/ed/20fd17937a4c00111d9cbe34e2b6027a diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/40f7512a42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/40f7512a42490011162d85db97d6efcd deleted file mode 100644 index 3a76c06..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/40f7512a42490011162d85db97d6efcd +++ /dev/null @@ -1,349 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/609b87fe3f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/609b87fe3f4600111c1cd5b6f02d115c deleted file mode 100644 index a39d1e2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/609b87fe3f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,55 +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 - */ - @Override - public boolean aggiungi(Object object) { - return false; - } - - // Metodo aggiungi - /* - * Questo metodo aggiunge realmente l'elemento di tipo - * Integer - */ - public boolean aggiungi(Integer integer) { - return super.aggiungi(integer); - } - - // Metodo ordina - /* - * Questo metodo permette il funzionamento dell'ordinamento - * tramite comparatore simulato - */ - @Override - protected int ordina(Object elemento1, Object elemento2) { - Integer i1 = (Integer)elemento1; - Integer i2 = (Integer)elemento2; - - if (i1 < i2) return -1; - if (i1 > i2) return 1; - return 0; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/90a8ecf642490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/90a8ecf642490011162d85db97d6efcd deleted file mode 100644 index 560dac0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/90a8ecf642490011162d85db97d6efcd +++ /dev/null @@ -1,375 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/a067004e40490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/a067004e40490011162d85db97d6efcd deleted file mode 100644 index f3ae19a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/a067004e40490011162d85db97d6efcd +++ /dev/null @@ -1,321 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 () - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/b0d991a19e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/b0d991a19e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..bbf2b27 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/b0d991a19e4c0011135ca7e1c9885acd @@ -0,0 +1,49 @@ +package jcf_map.exercise; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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. + myList.sort(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ee/204e902f1e4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/ee/204e902f1e4d0011135ca7e1c9885acd new file mode 100644 index 0000000..239f70f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ee/204e902f1e4d0011135ca7e1c9885acd @@ -0,0 +1,12 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ee/40954b8842490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ee/40954b8842490011162d85db97d6efcd deleted file mode 100644 index f107ac4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ee/40954b8842490011162d85db97d6efcd +++ /dev/null @@ -1,360 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ee/7082ca335a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ee/7082ca335a490011162d85db97d6efcd deleted file mode 100644 index 5cae35e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ee/7082ca335a490011162d85db97d6efcd +++ /dev/null @@ -1,440 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - BinaryNode currentNode = root; - - while (iterator.hasNext()) { - E currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - currentNode.setData(currentObject); - - if (wing == 0) { - // Left Wing - - } else { - // Right Wing - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ee/70cea9da32490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ee/70cea9da32490011162d85db97d6efcd deleted file mode 100644 index 1a9f452..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ee/70cea9da32490011162d85db97d6efcd +++ /dev/null @@ -1,251 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator iterator() { - return iteratorPreOrder(); - } - - // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) - - protected void itpreorder(BinaryNode node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode current; - - queueOfNodes.add(node); - while () { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/20dc66fd3f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/20dc66fd3f490011162d85db97d6efcd deleted file mode 100644 index 43cbd92..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/20dc66fd3f490011162d85db97d6efcd +++ /dev/null @@ -1,313 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode current; - Boolean flag; - - queueOfNodes.add(node); - flags.push(false); - - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - flag = flags.pop(); - - if () - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/80e1af142c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/80e1af142c470011162d85db97d6efcd deleted file mode 100644 index fdb9206..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/80e1af142c470011162d85db97d6efcd +++ /dev/null @@ -1,93 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/e0a02e6d5b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/e0a02e6d5b490011162d85db97d6efcd deleted file mode 100644 index aa4ec5e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/e0a02e6d5b490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/2005b9768e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/f/2005b9768e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..94783fb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f/2005b9768e4c0011135ca7e1c9885acd @@ -0,0 +1,5 @@ +package jcf_map.exercise; + +public class Dispari { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/70779f50ac4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/f/70779f50ac4c0011135ca7e1c9885acd new file mode 100644 index 0000000..6fa4b36 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f/70779f50ac4c0011135ca7e1c9885acd @@ -0,0 +1,37 @@ +package jcf_map.exercise; + +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 int ContaDuplicati(BinaryNode node) { + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + + return mappa; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/9018d048094c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/f/9018d048094c00111d9cbe34e2b6027a deleted file mode 100644 index e268fe2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f/9018d048094c00111d9cbe34e2b6027a +++ /dev/null @@ -1,32 +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 aHashSet = new HashSet(Arrays.asList(aArray)); - Set bHashSet = new HashSet(Arrays.asList(bArray)); - - - - } - - // Operazioni di insiemistica - - /* - * Per eseguire una unione, è conveniente - * adoperare la Set poichè l'implementazione - * TreeSet permette di non mantenere duplicati. - */ - public - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/909f1d4441490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f/909f1d4441490011162d85db97d6efcd deleted file mode 100644 index e0c59ef..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f/909f1d4441490011162d85db97d6efcd +++ /dev/null @@ -1,338 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/d051d3bf3e4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/f/d051d3bf3e4600111c1cd5b6f02d115c deleted file mode 100644 index 7c4b712..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f/d051d3bf3e4600111c1cd5b6f02d115c +++ /dev/null @@ -1,11 +0,0 @@ -package vettore_ordinabile; - -public class VettoriIntero { - - // Metodo costruttore default - /* - * Contiene solamente 10 elementi - * */ - public - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/d0bbb26982460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f/d0bbb26982460011162d85db97d6efcd deleted file mode 100644 index 63ee187..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f/d0bbb26982460011162d85db97d6efcd +++ /dev/null @@ -1,55 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode node) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/e03feef76a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f/e03feef76a460011162d85db97d6efcd deleted file mode 100644 index 2f62929..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f/e03feef76a460011162d85db97d6efcd +++ /dev/null @@ -1,100 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode oldParent = parent; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/f0f426f57d4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/f/f0f426f57d4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..b37b2d8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f/f0f426f57d4c00111d9cbe34e2b6027a @@ -0,0 +1,28 @@ +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) { + this.nominativo = nominativo; + this.cf = cf; + this.cittaResidenza = cittaResidenza; + } + + // Getters + public String nominativo() { + return nominativo; + } + + public String cf() { + return cf; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f0/00157520984c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/f0/00157520984c0011135ca7e1c9885acd new file mode 100644 index 0000000..15ee7c1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f0/00157520984c0011135ca7e1c9885acd @@ -0,0 +1,26 @@ +package jcf_map.exercise; + +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f1/b01a98c17d4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/f1/b01a98c17d4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..657ff4f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f1/b01a98c17d4c00111d9cbe34e2b6027a @@ -0,0 +1,17 @@ +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) { + this.nominativo = nominativo; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f2/30b5dad63c4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/30b5dad63c4d0011135ca7e1c9885acd new file mode 100644 index 0000000..3111b8f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/30b5dad63c4d0011135ca7e1c9885acd @@ -0,0 +1,179 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + return currentNode.data; + } + + public E set(int index, E item) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + + Node currentNode = head; + E oldData; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f2/40dc2e995c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/40dc2e995c490011162d85db97d6efcd deleted file mode 100644 index 93a7b2c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f2/40dc2e995c490011162d85db97d6efcd +++ /dev/null @@ -1,463 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - - } - - public void printLeaf(BinaryNode node) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f2/70e30c33aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/70e30c33aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..46e4e2e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/70e30c33aa4c0011135ca7e1c9885acd @@ -0,0 +1,25 @@ +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap mappa) { + if (node == null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f2/c0e6b7e86a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/c0e6b7e86a460011162d85db97d6efcd deleted file mode 100644 index 16124c0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f2/c0e6b7e86a460011162d85db97d6efcd +++ /dev/null @@ -1,98 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f3/402066662f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/402066662f470011162d85db97d6efcd deleted file mode 100644 index b70d6fd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f3/402066662f470011162d85db97d6efcd +++ /dev/null @@ -1,129 +0,0 @@ -package binary_tree; - -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f3/9087e70981460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/9087e70981460011162d85db97d6efcd deleted file mode 100644 index d3a1e4e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f3/9087e70981460011162d85db97d6efcd +++ /dev/null @@ -1,36 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(left.root, data, right.root); - size = 1 + left.size + right.size; - } - - // METODI - - public int size() { - return size; - } - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f3/c0601a52324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/c0601a52324d0011135ca7e1c9885acd new file mode 100644 index 0000000..050c951 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/c0601a52324d0011135ca7e1c9885acd @@ -0,0 +1,67 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + newNode.next = head; + head = newNode; + } + size++; + } + + public E removeFirst() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f4/009bcd6637470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/009bcd6637470011162d85db97d6efcd deleted file mode 100644 index 2aa55ee..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f4/009bcd6637470011162d85db97d6efcd +++ /dev/null @@ -1,232 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f4/102c55a07f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/102c55a07f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..dbf15fb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/102c55a07f4c00111d9cbe34e2b6027a @@ -0,0 +1,60 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f4/80c2243f7e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/80c2243f7e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..465399c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/80c2243f7e4c00111d9cbe34e2b6027a @@ -0,0 +1,44 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw NullPointerException(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f5/008473143b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/f5/008473143b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..b3b4ea6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f5/008473143b4d0011135ca7e1c9885acd @@ -0,0 +1,150 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index; i++) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f5/30a05556314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/f5/30a05556314600111c1cd5b6f02d115c deleted file mode 100644 index 17a4a40..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f5/30a05556314600111c1cd5b6f02d115c +++ /dev/null @@ -1,23 +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; - } - - /** - * Funzione aggiungi - */ - public boolean aggiungi() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f5/70bf8cfc34470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f5/70bf8cfc34470011162d85db97d6efcd deleted file mode 100644 index 23cf796..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f5/70bf8cfc34470011162d85db97d6efcd +++ /dev/null @@ -1,222 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - - queueOfNodes.add(node); - - while(!queueOfNodes.isEmpty()) { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f5/c0b3b4df30470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f5/c0b3b4df30470011162d85db97d6efcd deleted file mode 100644 index 9a75290..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f5/c0b3b4df30470011162d85db97d6efcd +++ /dev/null @@ -1,166 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - inorder(root, temporaryList); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f6/2036eb1143490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f6/2036eb1143490011162d85db97d6efcd deleted file mode 100644 index 689de91..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f6/2036eb1143490011162d85db97d6efcd +++ /dev/null @@ -1,376 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 - queueOfNodes.push(current); - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f6/3077d2e665460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f6/3077d2e665460011162d85db97d6efcd deleted file mode 100644 index 96e26b1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f6/3077d2e665460011162d85db97d6efcd +++ /dev/null @@ -1,7 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f6/30d4ee8d394d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/f6/30d4ee8d394d0011135ca7e1c9885acd new file mode 100644 index 0000000..d52bcae --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f6/30d4ee8d394d0011135ca7e1c9885acd @@ -0,0 +1,133 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f6/8039ed78aa4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/f6/8039ed78aa4c0011135ca7e1c9885acd new file mode 100644 index 0000000..4edd63e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f6/8039ed78aa4c0011135ca7e1c9885acd @@ -0,0 +1,30 @@ +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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, HashMap 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; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f6/80e131e0ab4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/f6/80e131e0ab4c0011135ca7e1c9885acd new file mode 100644 index 0000000..fa0747b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f6/80e131e0ab4c0011135ca7e1c9885acd @@ -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 Map ContaDuplicati(BinaryNode node) { + + } + + protected static Map ContaDuplicati(BinaryNode node, TreeMap 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(data, 1); + else mappa.put(data, mappa.get(data) + 1); + + return mappa; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f7/30a87d1440490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/30a87d1440490011162d85db97d6efcd deleted file mode 100644 index b0c36eb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f7/30a87d1440490011162d85db97d6efcd +++ /dev/null @@ -1,319 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f7/501f784e35470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/501f784e35470011162d85db97d6efcd deleted file mode 100644 index 9ede96f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f7/501f784e35470011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode current; - - queueOfNodes.add(node); - while(!queueOfNodes.isEmpty()) { - current = queueOfNodes.remove(); - - if () - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f7/60d0454131470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/60d0454131470011162d85db97d6efcd deleted file mode 100644 index c6494ea..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f7/60d0454131470011162d85db97d6efcd +++ /dev/null @@ -1,168 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - inorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE POSTORDER - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f7/c096101a42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/c096101a42490011162d85db97d6efcd deleted file mode 100644 index ada265b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f7/c096101a42490011162d85db97d6efcd +++ /dev/null @@ -1,345 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - itinorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f8/306865ce2f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/306865ce2f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..bf0e0ef --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/306865ce2f4d0011135ca7e1c9885acd @@ -0,0 +1,54 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f8/30f080e17e4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/30f080e17e4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..1dfa47a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/30f080e17e4c00111d9cbe34e2b6027a @@ -0,0 +1,50 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + for (String s : archivio.keySet()) { + Cliente currentCliente = archivio.get(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f8/7084a7d650490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/7084a7d650490011162d85db97d6efcd deleted file mode 100644 index d361cae..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f8/7084a7d650490011162d85db97d6efcd +++ /dev/null @@ -1,409 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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. - */ - public LinkedBinaryTree(List objectList) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f8/d07b20925b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/d07b20925b490011162d85db97d6efcd deleted file mode 100644 index aa4ec5e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f8/d07b20925b490011162d85db97d6efcd +++ /dev/null @@ -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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - BinaryNode currentNode = root; - E currentObject = iterator.next(); - currentNode.setData(currentObject); - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f8/e06312e79b4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/e06312e79b4c0011135ca7e1c9885acd new file mode 100644 index 0000000..9092612 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/e06312e79b4c0011135ca7e1c9885acd @@ -0,0 +1,35 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + if (myList.isEmpty()) return true; + // Verifica che la lista si pari o dispari + HashMap valori = new HashMap(); + + Iterator it = myList.iterator(); + while (it.hasNext()) { + E current = it.next(); + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/109ef43d77460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/109ef43d77460011162d85db97d6efcd deleted file mode 100644 index 1c0a9ac..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/109ef43d77460011162d85db97d6efcd +++ /dev/null @@ -1,149 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; - if (this.left != null && !left.equals(otherType.left)) return false; - if (this.right != null && !right.equals(otherType.right)) return false; - return true; - } - - public int levelOf() { - int d = 0; - BinaryNode cur = this; - - while (cur.parent != null){ - d++; - cur = cur.parent; - } - - return d; - } - - public int height() { - if (left == null && right == null) return 0; - - int hLeft = (left == null) ? 0 : left.height(); - int hRight = (right == null) ? 0 : right.height(); - - return Math.max(hLeft + hRight) + 1; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/50697e5a374d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/50697e5a374d0011135ca7e1c9885acd new file mode 100644 index 0000000..93a8e62 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/50697e5a374d0011135ca7e1c9885acd @@ -0,0 +1,108 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/6073767d42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/6073767d42490011162d85db97d6efcd deleted file mode 100644 index d48fe2c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/6073767d42490011162d85db97d6efcd +++ /dev/null @@ -1,357 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>();) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/608405b275460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/608405b275460011162d85db97d6efcd deleted file mode 100644 index 34cbefd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/608405b275460011162d85db97d6efcd +++ /dev/null @@ -1,125 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode 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; - - if (!data.equals(otherType.equals) || ) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/e0eb29be024c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/e0eb29be024c00111d9cbe34e2b6027a deleted file mode 100644 index 5cc426f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/e0eb29be024c00111d9cbe34e2b6027a +++ /dev/null @@ -1,20 +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 lista = new ArrayList(); - - lista.add(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/f047f2ce084c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/f047f2ce084c00111d9cbe34e2b6027a deleted file mode 100644 index 03d2687..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/f047f2ce084c00111d9cbe34e2b6027a +++ /dev/null @@ -1,21 +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 aHashSet = new HashSet(Arrays.asList(aArray)); - Set bHashSet = new HashSet(Arrays.asList(bArray)); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/f0c05c943f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/f0c05c943f490011162d85db97d6efcd deleted file mode 100644 index c9d5e1b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/f0c05c943f490011162d85db97d6efcd +++ /dev/null @@ -1,309 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode current; - - queueOfNodes.add(node); - while (!queueOfNodes.isEmpty()) { - current = queueOfNodes.pop(); - - // Aggiungiamo il nodo di destra - if (current.getRight() != null) queueOfNodes.push(current.getRight()); - // Aggiungiamo - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/000757b35c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/000757b35c490011162d85db97d6efcd deleted file mode 100644 index c50e715..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/000757b35c490011162d85db97d6efcd +++ /dev/null @@ -1,463 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - - /* - * II parziale 2022/2023 - * Realizzare un metodo costruttore della classe LinkedBinaryTree 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 objectList) { - if (objectList == null) throw new NullPointerException(); - - Iterator iterator = objectList.iterator(); - if (!iterator.hasNext()) return; - - E currentObject = iterator.next(); - root = new BinaryNode(currentObject); - BinaryNode currentNode = root; - currentNode.setData(currentObject); - size = 1; - - while (iterator.hasNext()) { - currentObject = iterator.next(); - int wing = (int) (Math.random() * 2); - - BinaryNode newNode = new BinaryNode(currentObject); - - if (wing == 0) { - // Left - newNode.setParentAsLeftChild(currentNode); - } else { - // Right - newNode.setParentAsRightChild(currentNode); - } - - currentNode = newNode; - size++; - } - } - - /* - * II parziale 2022 - * Si aggiunga alla classe LinkedBinaryTree un metodo - * che stampa le foglie dall'albero corrente (da sinistra verso destra) - */ - public void printLeaf() { - - } - - public void printLeaf(BinaryNode node) { - if (node == null) return; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/604dee4e9e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/604dee4e9e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..7d899a4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/604dee4e9e4c0011135ca7e1c9885acd @@ -0,0 +1,44 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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); + } + for (E chiave : valori.keySet()) { + + } + // Controllo short circuit di disparità + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/70bdf6a57d4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/70bdf6a57d4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..c9fe900 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/70bdf6a57d4c00111d9cbe34e2b6027a @@ -0,0 +1,10 @@ +package parziale.p191108; + +public class Cliente { + + // Variabili di instanza + private String nominativo; + private String cf; + private String cittaResidenza; + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/80c0277137470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/80c0277137470011162d85db97d6efcd deleted file mode 100644 index 5c86188..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/80c0277137470011162d85db97d6efcd +++ /dev/null @@ -1,233 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/9019649f43490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/9019649f43490011162d85db97d6efcd deleted file mode 100644 index 05478d9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/9019649f43490011162d85db97d6efcd +++ /dev/null @@ -1,397 +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 implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode 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 iteratorLevelOrder() { - ArrayList temporaryList = new ArrayList(); - levelorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE - - public Iterator 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - BinaryNode 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 ititeratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - Stack> queueOfNodes = new Stack>(); - Stack flags = new Stack(); - - BinaryNode 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 ititeratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - itpostorder(root, temporaryList); - return temporaryList.iterator(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/a062590781460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/a062590781460011162d85db97d6efcd deleted file mode 100644 index 32e8b05..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/a062590781460011162d85db97d6efcd +++ /dev/null @@ -1,32 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(left.root, data, right.root); - size = 1 + left.size + right.size; - } - - // METODI - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/d00ff3c52c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/d00ff3c52c470011162d85db97d6efcd deleted file mode 100644 index da64fad..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/d00ff3c52c470011162d85db97d6efcd +++ /dev/null @@ -1,101 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode resNode; - resNode = find(targetElement, root.getLeft()); - if (resNode == null) resNode = find(targetElement, root.getRight()); - return resNode; - } - - public boolean remove(E targetElement) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/0056d117304d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/0056d117304d0011135ca7e1c9885acd new file mode 100644 index 0000000..d81522c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/0056d117304d0011135ca7e1c9885acd @@ -0,0 +1,59 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 = + } else { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/0099c5689e4c0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/0099c5689e4c0011135ca7e1c9885acd new file mode 100644 index 0000000..862eb20 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/0099c5689e4c0011135ca7e1c9885acd @@ -0,0 +1,45 @@ +package jcf_map.exercise; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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); + } + for (E chiave : valori.keySet()) { + int valore = valori.get(chiave); + if (valore % 2 == 0) return false; + } + // Controllo short circuit di disparità + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/20f4423a7f4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/20f4423a7f4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..2ef61fd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/20f4423a7f4c00111d9cbe34e2b6027a @@ -0,0 +1,53 @@ +package parziale.p191108; + +import java.util.HashMap; +import java.util.Map; + +public class Archivio { + + // Variabili di instanza + private Map archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + for (String s : archivio.keySet()) { + Cliente currentCliente = archivio.get(s); + String currentCitta = currentCliente.cittaResidenza(); + + if (currentCitta.equals(citta)) autoSelezionate.put(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/506acab431470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/506acab431470011162d85db97d6efcd deleted file mode 100644 index f88e94e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/506acab431470011162d85db97d6efcd +++ /dev/null @@ -1,185 +0,0 @@ -package binary_tree; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 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 node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - 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 node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/e08b4a326a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/e08b4a326a460011162d85db97d6efcd deleted file mode 100644 index b7a6ad8..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/e08b4a326a460011162d85db97d6efcd +++ /dev/null @@ -1,78 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (this.parent == null) return null; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/f04a91032d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/f04a91032d470011162d85db97d6efcd deleted file mode 100644 index ceb4477..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/f04a91032d470011162d85db97d6efcd +++ /dev/null @@ -1,105 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode 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 removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode 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 temp = find(targetElement, root); - if (temp != null) { - - } return false; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fc/00921b881b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/00921b881b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..8a920bf --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/00921b881b4d0011135ca7e1c9885acd @@ -0,0 +1,34 @@ +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 mappaAssociazione = new TreeMap(); + + companyMap.put("Rossi Marco", 8); + companyMap.put("Bianchi Luca", 14); + companyMap.put("Esposito Andrea", 6); + companyMap.put("Ferrari Matteo", 6); + companyMap.put("Romano Giulia", 14); + companyMap.put("Ricci Alessia", 6); + + System.out.println("Stampa mappa associazione: "); + System.out.println(); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fc/0096e9523f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/0096e9523f4600111c1cd5b6f02d115c deleted file mode 100644 index d365567..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fc/0096e9523f4600111c1cd5b6f02d115c +++ /dev/null @@ -1,37 +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 - */ - @Override - public boolean aggiungi(Object object) { - return false; - } - - // Metodo aggiungi - /* - * Questo metodo aggiunge realmente l'elemento di tipo - * Integer - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fc/70397e4f2f4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/70397e4f2f4d0011135ca7e1c9885acd new file mode 100644 index 0000000..6ea07a3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/70397e4f2f4d0011135ca7e1c9885acd @@ -0,0 +1,40 @@ +package list.mylinkedlist; + +import java.util.List; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node tail = null; + private int size = 0; + + // Costruttore + public MyLinkedList() {} + + // Metodi + + public boolean isEmpty() { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fd/109c03703b4d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/109c03703b4d0011135ca7e1c9885acd new file mode 100644 index 0000000..281eab2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/109c03703b4d0011135ca7e1c9885acd @@ -0,0 +1,154 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node nextNode = prevNode.next; + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fd/b09e96bb324d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/b09e96bb324d0011135ca7e1c9885acd new file mode 100644 index 0000000..09a40d3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/b09e96bb324d0011135ca7e1c9885acd @@ -0,0 +1,74 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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(item, null, null); + } else { + Node newNode = new Node(item, null, null); + head.prev = newNode; + newNode.next = head; + head = newNode; + } + size++; + } + + public E removeFirst() { + if (head == null) throw new NoSuchElementException(); + E tmp; + if (size == 1) { + E tmp = head.data; + head = tail = null; + return tmp; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fd/d0051c1d80460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/d0051c1d80460011162d85db97d6efcd deleted file mode 100644 index 8dc273a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fd/d0051c1d80460011162d85db97d6efcd +++ /dev/null @@ -1,17 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - - // METODI - - @Override - public boolean isEmpty() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fd/d05b94546b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/d05b94546b460011162d85db97d6efcd deleted file mode 100644 index a528534..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fd/d05b94546b460011162d85db97d6efcd +++ /dev/null @@ -1,105 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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 getLeft() { - return left; - } - - public BinaryNode getRight() { - return right; - } - - public BinaryNode 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 left) { - this.left = left; - if (this.left != null) this.left.parent = this; - } - - public void setRight(BinaryNode right) { - this.right = right; - if (this.right != null) this.right.parent = this; - } - - public BinaryNode setParentAsLeftChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldLeft = this.parent.left; - this.parent.left = this; - - return oldLeft; - } - - public BinaryNode setParentAsRightChild(BinaryNode parent) { - if (parent == null) return null; - - this.parent = parent; - BinaryNode oldRight = this.parent.right; - this.parent.right = this; - - return oldRight; - } - - public BinaryNode setAsRoot() { - if (parent == null) return null; - - BinaryNode oldParent = parent; - parent = null; - - if (oldParent.left == this) oldParent.left = null; - - return oldParent; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fd/e06265aa82460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/e06265aa82460011162d85db97d6efcd deleted file mode 100644 index d0c2e74..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fd/e06265aa82460011162d85db97d6efcd +++ /dev/null @@ -1,55 +0,0 @@ -package binary_tree; - -public class LinkedBinaryTree implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(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 getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode node) { - if (node.getLeft() == null && node.getRight() == null) return 0; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fe/a06feecb7d4c00111d9cbe34e2b6027a b/.metadata/.plugins/org.eclipse.core.resources/.history/fe/a06feecb7d4c00111d9cbe34e2b6027a new file mode 100644 index 0000000..5905550 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fe/a06feecb7d4c00111d9cbe34e2b6027a @@ -0,0 +1,19 @@ +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) { + this.nominativo = nominativo; + this.cf = cf; + this.cittaResidenza = cittaResidenza; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ff/209a7ab667460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ff/209a7ab667460011162d85db97d6efcd deleted file mode 100644 index 2238ce8..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ff/209a7ab667460011162d85db97d6efcd +++ /dev/null @@ -1,34 +0,0 @@ -package binary_tree; - -public class BinaryNode { - - // VARIABILI - - private BinaryNode left, right, parent; - private E data; - - // COSTRUTTORI - - public BinaryNode(E data) { - this.data = data; - - left = right = parent = null; - } - - public BinaryNode(BinaryNode left, E data, BinaryNode 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) - } - - // METODI - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ff/a0c1cb17394d0011135ca7e1c9885acd b/.metadata/.plugins/org.eclipse.core.resources/.history/ff/a0c1cb17394d0011135ca7e1c9885acd new file mode 100644 index 0000000..a930e41 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ff/a0c1cb17394d0011135ca7e1c9885acd @@ -0,0 +1,132 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.markers.snap b/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.markers.snap index 464a957..08e6b18 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.markers.snap and b/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.markers.snap differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.syncinfo.snap b/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.syncinfo.snap index 464a957..08e6b18 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.syncinfo.snap and b/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.syncinfo.snap differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/af/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/af/history.index deleted file mode 100644 index e04a951..0000000 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/af/history.index and /dev/null differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/79/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/79/history.index deleted file mode 100644 index 9657852..0000000 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/79/history.index and /dev/null differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/90/d8/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/90/d8/history.index new file mode 100644 index 0000000..45c62a6 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/90/d8/history.index differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/92/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/92/history.index deleted file mode 100644 index bd28b16..0000000 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/92/history.index and /dev/null differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/96/d8/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/96/d8/history.index new file mode 100644 index 0000000..5427232 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/96/d8/history.index differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/9e/1d/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/9e/1d/history.index new file mode 100644 index 0000000..df90cf7 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/9e/1d/history.index differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/a/72/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/a/72/history.index index 27c1dd0..d592fa0 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/a/72/history.index and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/a/72/history.index differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/a/c0/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/a/c0/history.index new file mode 100644 index 0000000..e1627b8 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/a/c0/history.index differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/e4/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/e4/history.index index adffa50..190fcdf 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/e4/history.index and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/e4/history.index differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/history.index deleted file mode 100644 index 8733198..0000000 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/history.index and /dev/null differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.markers b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.markers index 92e43e6..fac457a 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.markers and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.markers differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.markers.snap b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.markers.snap index 77a20c2..c68038d 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.markers.snap and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.markers.snap differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.syncinfo.snap b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.syncinfo.snap index 64b1875..08e6b18 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.syncinfo.snap and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.syncinfo.snap differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.root/.indexes/properties.index b/.metadata/.plugins/org.eclipse.core.resources/.root/.indexes/properties.index index a0af7b2..cf87a86 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.root/.indexes/properties.index and b/.metadata/.plugins/org.eclipse.core.resources/.root/.indexes/properties.index differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.root/.markers.snap b/.metadata/.plugins/org.eclipse.core.resources/.root/.markers.snap index 5ed9b35..0ef1fa6 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.root/.markers.snap and b/.metadata/.plugins/org.eclipse.core.resources/.root/.markers.snap differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.root/1.tree b/.metadata/.plugins/org.eclipse.core.resources/.root/1.tree deleted file mode 100644 index 4149426..0000000 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.root/1.tree and /dev/null differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.root/2.tree b/.metadata/.plugins/org.eclipse.core.resources/.root/2.tree new file mode 100644 index 0000000..40d82a8 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.root/2.tree differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources b/.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources index 3d6c9b7..66adea3 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources and b/.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/1.snap b/.metadata/.plugins/org.eclipse.core.resources/1.snap deleted file mode 100644 index e0f4dad..0000000 Binary files a/.metadata/.plugins/org.eclipse.core.resources/1.snap and /dev/null differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/2.snap b/.metadata/.plugins/org.eclipse.core.resources/2.snap new file mode 100644 index 0000000..f7a53ad Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/2.snap differ diff --git a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.launching.prefs b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.launching.prefs index 251d385..ed2fe85 100644 --- a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.launching.prefs +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.launching.prefs @@ -1,2 +1,2 @@ eclipse.preferences.version=1 -org.eclipse.jdt.launching.PREF_VM_XML=\r\n\r\n \r\n \r\n \r\n\r\n +org.eclipse.jdt.launching.PREF_VM_XML=\r\n\r\n \r\n \r\n \r\n \r\n\r\n diff --git a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs index a815d4c..ff92bb2 100644 --- a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs @@ -9,9 +9,7 @@ content_assist_parameters_background,defaultValueBeforeOverriddenFromCSS=255,255 content_assist_parameters_foreground=238,238,238 content_assist_parameters_foreground,defaultValueBeforeOverriddenFromCSS=0,0,0 content_assist_proposals_background=255,255,255 -content_assist_proposals_background,defaultValueBeforeOverriddenFromCSS=255,255,255 content_assist_proposals_foreground=0,0,0 -content_assist_proposals_foreground,defaultValueBeforeOverriddenFromCSS=0,0,0 eclipse.preferences.version=1 java_bracket=249,250,244 java_bracket,defaultValueBeforeOverriddenFromCSS=0,0,0 diff --git a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.ide.prefs b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.ide.prefs index 489e2c7..6a24591 100644 --- a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.ide.prefs +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.ide.prefs @@ -1,4 +1,4 @@ eclipse.preferences.version=1 -platformState=943974497357990 +platformState=949639751129723 quickStart=false tipsAndTricks=true diff --git a/.metadata/.plugins/org.eclipse.debug.core/.launches/CompanyMap.launch b/.metadata/.plugins/org.eclipse.debug.core/.launches/CompanyMap.launch new file mode 100644 index 0000000..3a561b9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.debug.core/.launches/CompanyMap.launch @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/.metadata/.plugins/org.eclipse.debug.ui/launchConfigurationHistory.xml b/.metadata/.plugins/org.eclipse.debug.ui/launchConfigurationHistory.xml index 1ac4ead..9e8cc5d 100644 --- a/.metadata/.plugins/org.eclipse.debug.ui/launchConfigurationHistory.xml +++ b/.metadata/.plugins/org.eclipse.debug.ui/launchConfigurationHistory.xml @@ -2,6 +2,8 @@ + + @@ -10,7 +12,6 @@ - @@ -20,6 +21,8 @@ + + @@ -28,7 +31,6 @@ - @@ -38,6 +40,8 @@ + + @@ -46,7 +50,6 @@ - diff --git a/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi b/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi index 5cd2fd8..b1fd3e5 100644 --- a/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi +++ b/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi @@ -1,18 +1,19 @@ - - + + activeSchemeId:org.eclipse.ui.defaultAcceleratorConfiguration - + - - + + topLevel - - - + shellMaximized + + + persp.actionSet:org.eclipse.mylyn.tasks.ui.navigation persp.actionSet:org.eclipse.ui.cheatsheets.actionSet @@ -79,2469 +80,2481 @@ persp.editorOnboardingCommand:Show Key Assist$$$Ctrl+Shift+L persp.editorOnboardingCommand:New$$$Ctrl+N persp.editorOnboardingCommand:Open Type$$$Ctrl+Shift+T - - - + + + org.eclipse.e4.primaryNavigationStack - + View categoryTag:Java - + View categoryTag:Java - + View categoryTag:General - + View categoryTag:Java - - + + View categoryTag:Git - - - - + + + + org.eclipse.e4.secondaryNavigationStack Minimized - + View categoryTag:General - + View categoryTag:General - + View categoryTag:General - + View categoryTag:Mylyn - + View categoryTag:Java - + View categoryTag:Ant - + org.eclipse.e4.secondaryDataStack Oomph - + View categoryTag:General - + View categoryTag:Java - + View categoryTag:Java - + View categoryTag:General - + View categoryTag:General - + View categoryTag:General - + View categoryTag:Terminal - - View - categoryTag:Oomph - NoRestore - - - View - categoryTag:Oomph - NoRestore - - + View categoryTag:General + + View + categoryTag:Oomph + NoRestore + + + View + categoryTag:Oomph + NoRestore + - - + + View categoryTag:Help - + View categoryTag:General - + View categoryTag:Help - + View categoryTag:Help - + View categoryTag:General - + ViewMenu menuContribution:menu - + - + View categoryTag:Help - - + + EditorStack org.eclipse.e4.primaryDataStack active noFocus - - + + Editor removeOnHide org.eclipse.jdt.ui.CompilationUnitEditor - - - Editor - removeOnHide - org.eclipse.jdt.ui.CompilationUnitEditor - - - + + Editor removeOnHide org.eclipse.jdt.ui.CompilationUnitEditor active + + + Editor + removeOnHide + org.eclipse.jdt.ui.CompilationUnitEditor + + + + Editor + removeOnHide + org.eclipse.jdt.ui.CompilationUnitEditor + + + + Editor + removeOnHide + org.eclipse.jdt.ui.CompilationUnitEditor + - + View categoryTag:Java - + ViewMenu menuContribution:menu - + - + View categoryTag:Java - + View categoryTag:General - + View categoryTag:General - + ViewMenu menuContribution:menu - + - + View categoryTag:Java - + View categoryTag:Java - + View categoryTag:General - + View categoryTag:General - + ViewMenu menuContribution:menu - + - + View categoryTag:General - + View categoryTag:General - + View categoryTag:General - + ViewMenu menuContribution:menu - + - + View categoryTag:General - + View categoryTag:General - + View categoryTag:Mylyn - + View categoryTag:Terminal - + View categoryTag:Java - + View categoryTag:Git - + View categoryTag:Java - + View categoryTag:Ant - + View categoryTag:Oomph NoRestore - + ViewMenu menuContribution:menu - + - + View categoryTag:Oomph NoRestore - + ViewMenu menuContribution:menu - + - - + + toolbarSeparator - + - + Draggable - + - + toolbarSeparator - + - + Draggable - - + + - + toolbarSeparator - + - + Draggable - + Draggable - + Draggable - + Draggable - + toolbarSeparator - + - + Draggable - + - + Draggable - + toolbarSeparator - + - + toolbarSeparator - + - + Draggable - + stretch SHOW_RESTORE_MENU - + Draggable HIDEABLE SHOW_RESTORE_MENU - - + + stretch - + Draggable - + Draggable - - + + TrimStack Draggable - + TrimStack Draggable - - + + TrimStack Draggable - + TrimStack Draggable - + TrimStack Draggable - - - - - - + + + + + + platform:cocoa - + platform:cocoa - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + platform:win32 - - - + + + - - - - - - - - - + + + + + + + + + platform:cocoa - - - - - - - - - - - - - - + + + + + + + + + + + + + + platform:cocoa - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - - + + - - - - - - - - + + + + + + + + - - - + + + - - - + + + - - - - - + + + + + - - + + - - - + + + - - - + + + - - - - - - - - - + + + + + + + + + - - + + - - + + - - - - - - - - - - - - + + + + + + + + + + + + - - - - + + + + - - + + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - - + + + + + + + + - - + + platform:win32 - - - - - - - + + + + + + + - - - - - - + + + + + + - - - - - - - - + + + + + + + + - - + + - - + + - - + + - - + + - - - + + + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - - + + - - + + - - - - + + + + - - + + - - - - - - + + + + + + - - - - + + + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - - - - - - - - + + + + + + + + + - - - - - + + + + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Editor removeOnHide - + View categoryTag:Ant - + View categoryTag:Gradle - + View categoryTag:Gradle - + View categoryTag:Debug - + View categoryTag:Debug - + View categoryTag:Debug - + View categoryTag:Debug - + View categoryTag:Debug - + View categoryTag:Debug - + View categoryTag:Debug - + View categoryTag:Debug - + View categoryTag:Java - + View categoryTag:Git - + View categoryTag:Git - + View categoryTag:Git - + View categoryTag:Git NoRestore - + View categoryTag:Git - + View categoryTag:Help - + View categoryTag:Java - + View categoryTag:Java - + View categoryTag:Debug - + View categoryTag:Java - + View categoryTag:Java - + View categoryTag:Java - + View categoryTag:Java Browsing - + View categoryTag:Java Browsing - + View categoryTag:Java Browsing - + View categoryTag:Java Browsing - + View categoryTag:Java - + View categoryTag:General - + View categoryTag:Java - + View categoryTag:Java - + View categoryTag:Language Servers - + View categoryTag:Language Servers - + View categoryTag:Language Servers - + View categoryTag:Maven - + View categoryTag:Maven - + View categoryTag:Maven - + View categoryTag:Mylyn - + View categoryTag:Mylyn - + View categoryTag:Mylyn - + View categoryTag:Mylyn - + View categoryTag:Mylyn - + View categoryTag:Mylyn - + View categoryTag:Oomph - + View categoryTag:Oomph NoRestore - + View categoryTag:Plug-in Development - + View categoryTag:General - + View categoryTag:Version Control (Team) - + View categoryTag:Version Control (Team) - + View categoryTag:Terminal - + View categoryTag:Help - + View categoryTag:General - + View categoryTag:General - + View categoryTag:Help - + View categoryTag:General - + View categoryTag:General - + View categoryTag:General - + View categoryTag:General - + View categoryTag:General - + View categoryTag:General - + View categoryTag:General - + View categoryTag:General - + View categoryTag:General - + View categoryTag:General - + View categoryTag:General - - + + glue move_after:PerspectiveSpacer SHOW_RESTORE_MENU - + move_after:Spacer Glue HIDEABLE SHOW_RESTORE_MENU - + glue move_after:SearchField SHOW_RESTORE_MENU - - - - - + + + + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + - - - - - - - - - + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + - - - - - - - + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - - - + + + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - - - - + + + + - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - + + + + + - - - + + + - - - - - - - - + + + + + + + + - - - - - + + + + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - - - - - + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - + + + + + - - - - - - - - - + + + + + + + + + - - - - - + + + + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - - - - - - - - - - - - + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.metadata/.plugins/org.eclipse.jdt.core/1865797976.index b/.metadata/.plugins/org.eclipse.jdt.core/1865797976.index index 4c3a01e..a4d58f6 100644 Binary files a/.metadata/.plugins/org.eclipse.jdt.core/1865797976.index and b/.metadata/.plugins/org.eclipse.jdt.core/1865797976.index differ diff --git a/.metadata/.plugins/org.eclipse.jdt.core/2876837992.index b/.metadata/.plugins/org.eclipse.jdt.core/2876837992.index index 8b59614..6baba15 100644 Binary files a/.metadata/.plugins/org.eclipse.jdt.core/2876837992.index and b/.metadata/.plugins/org.eclipse.jdt.core/2876837992.index differ diff --git a/.metadata/.plugins/org.eclipse.jdt.core/externalFilesCache b/.metadata/.plugins/org.eclipse.jdt.core/externalFilesCache index 5119c62..f3be2a1 100644 Binary files a/.metadata/.plugins/org.eclipse.jdt.core/externalFilesCache and b/.metadata/.plugins/org.eclipse.jdt.core/externalFilesCache differ diff --git a/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt b/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt index 7f28ed1..c1bda67 100644 --- a/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt +++ b/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt @@ -1,4 +1,3 @@ INDEX VERSION 1.134+C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.jdt.core 2555619149.index 1865797976.index -2876837992.index diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/dialog_settings.xml b/.metadata/.plugins/org.eclipse.jdt.ui/dialog_settings.xml index d767529..1ab2a78 100644 --- a/.metadata/.plugins/org.eclipse.jdt.ui/dialog_settings.xml +++ b/.metadata/.plugins/org.eclipse.jdt.ui/dialog_settings.xml @@ -13,4 +13,28 @@
+
+ + + + + +
+
+ +
+
+ + + + + +
+
+ +
+
+ + +
diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png index f5c6e73..3ac89df 100644 Binary files a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/10.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/10.png new file mode 100644 index 0000000..db1121a Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/10.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png index 4210c2a..6630b18 100644 Binary files a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/3.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/3.png index 0302b8c..f5c6e73 100644 Binary files a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/3.png and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/3.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/4.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/4.png index 3ac89df..1e41e1f 100644 Binary files a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/4.png and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/4.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/5.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/5.png index dfbdbb0..469abaa 100644 Binary files a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/5.png and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/5.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/6.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/6.png new file mode 100644 index 0000000..f6e5026 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/6.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/7.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/7.png new file mode 100644 index 0000000..dfbdbb0 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/7.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/8.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/8.png new file mode 100644 index 0000000..4210c2a Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/8.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/9.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/9.png new file mode 100644 index 0000000..05b280b Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/9.png differ diff --git a/.metadata/.plugins/org.eclipse.m2e.logback/0.log b/.metadata/.plugins/org.eclipse.m2e.logback/0.log index 0ffa9f5..f906d07 100644 --- a/.metadata/.plugins/org.eclipse.m2e.logback/0.log +++ b/.metadata/.plugins/org.eclipse.m2e.logback/0.log @@ -2,3 +2,4 @@ 2026-05-02 19:00:17,148 [Worker-7: Loading available Gradle versions] INFO o.e.b.c.i.u.g.PublishedGradleVersions - Gradle version information cache is up-to-date. Trying to read. 2026-05-02 22:23:21,340 [Worker-2: Loading available Gradle versions] INFO o.e.b.c.i.u.g.PublishedGradleVersions - Gradle version information cache is up-to-date. Trying to read. 2026-05-09 20:34:37,650 [Worker-1: Loading available Gradle versions] INFO o.e.b.c.i.u.g.PublishedGradleVersions - Gradle version information cache is up-to-date. Trying to read. +2026-05-10 01:32:01,791 [Worker-5: Loading available Gradle versions] INFO o.e.b.c.i.u.g.PublishedGradleVersions - Gradle version information cache is up-to-date. Trying to read. diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Crashpad/settings.dat b/.metadata/.plugins/org.eclipse.swt/EBWebView/Crashpad/settings.dat index 9f4511f..4083a1e 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Crashpad/settings.dat and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Crashpad/settings.dat differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/BrowsingTopicsState b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/BrowsingTopicsState index 5bd322c..192cbf1 100644 --- a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/BrowsingTopicsState +++ b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/BrowsingTopicsState @@ -8,5 +8,5 @@ "top_topics_and_observing_domains": [ ] } ], "hex_encoded_hmac_key": "3DA604280665A873D0EA6D316B3A72B9F419BDC719033123A3E96DBB0342BE2C", - "next_scheduled_calculation_time": "13423430092943182" + "next_scheduled_calculation_time": "13423430092943205" } diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Cache/Cache_Data/data_0 b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Cache/Cache_Data/data_0 index 11699c2..0f2e114 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Cache/Cache_Data/data_0 and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Cache/Cache_Data/data_0 differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Cache/Cache_Data/data_1 b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Cache/Cache_Data/data_1 index 74df021..b7fbb0c 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Cache/Cache_Data/data_1 and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Cache/Cache_Data/data_1 differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Cache/Cache_Data/data_3 b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Cache/Cache_Data/data_3 index 41f46a4..4a76857 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Cache/Cache_Data/data_3 and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Cache/Cache_Data/data_3 differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Cache/Cache_Data/f_000003 b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Cache/Cache_Data/f_000004 similarity index 100% rename from .metadata/.plugins/org.eclipse.swt/EBWebView/Default/Cache/Cache_Data/f_000003 rename to .metadata/.plugins/org.eclipse.swt/EBWebView/Default/Cache/Cache_Data/f_000004 diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Code Cache/js/index-dir/the-real-index b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Code Cache/js/index-dir/the-real-index index 552b36f..a71290d 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Code Cache/js/index-dir/the-real-index and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Code Cache/js/index-dir/the-real-index differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/DIPS b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/DIPS index 159e90a..cd010b2 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/DIPS and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/DIPS differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/DIPS-wal b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/DIPS-wal index 5139feb..8c8188e 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/DIPS-wal and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/DIPS-wal differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/DawnGraphiteCache/data_1 b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/DawnGraphiteCache/data_1 index d308a1c..a0957ea 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/DawnGraphiteCache/data_1 and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/DawnGraphiteCache/data_1 differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/DawnWebGPUCache/data_1 b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/DawnWebGPUCache/data_1 index 69c3eb0..62d956a 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/DawnWebGPUCache/data_1 and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/DawnWebGPUCache/data_1 differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/EdgeJourneys/EdgeJourneys.db b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/EdgeJourneys/EdgeJourneys.db index 9096fc9..d2b419a 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/EdgeJourneys/EdgeJourneys.db and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/EdgeJourneys/EdgeJourneys.db differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/EdgeJourneys/EdgeJourneys.db-wal b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/EdgeJourneys/EdgeJourneys.db-wal index 59113a6..e69de29 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/EdgeJourneys/EdgeJourneys.db-wal and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/EdgeJourneys/EdgeJourneys.db-wal differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Extension State/LOG.old b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Extension State/LOG.old index 4aa92b3..f438ea6 100644 --- a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Extension State/LOG.old +++ b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Extension State/LOG.old @@ -1,2 +1,3 @@ -2026/05/09-20:34:50.298 1b8c Creating DB C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Extension State since it was missing. -2026/05/09-20:34:50.301 1b8c Reusing MANIFEST C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Extension State/MANIFEST-000001 +2026/05/10-01:32:20.887 2aa0 Reusing MANIFEST C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Extension State/MANIFEST-000001 +2026/05/10-01:32:20.887 2aa0 Recovering log #3 +2026/05/10-01:32:20.888 2aa0 Reusing old log C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Extension State/000003.log diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/GPUCache/data_1 b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/GPUCache/data_1 index 5ed3d7f..f29b340 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/GPUCache/data_1 and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/GPUCache/data_1 differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/History b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/History index 84fd546..c2f024b 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/History and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/History differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Local Storage/leveldb/000003.log b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Local Storage/leveldb/000003.log index a09fe0b..dacf3f2 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Local Storage/leveldb/000003.log and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Local Storage/leveldb/000003.log differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Local Storage/leveldb/LOG b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Local Storage/leveldb/LOG index e69de29..58be978 100644 --- a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Local Storage/leveldb/LOG +++ b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Local Storage/leveldb/LOG @@ -0,0 +1,3 @@ +2026/05/10-18:30:40.011 292c Reusing MANIFEST C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Local Storage\leveldb/MANIFEST-000001 +2026/05/10-18:30:40.021 292c Recovering log #3 +2026/05/10-18:30:40.022 292c Reusing old log C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Local Storage\leveldb/000003.log diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Local Storage/leveldb/LOG.old b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Local Storage/leveldb/LOG.old index d69ea99..ec86aee 100644 --- a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Local Storage/leveldb/LOG.old +++ b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Local Storage/leveldb/LOG.old @@ -1,2 +1,3 @@ -2026/05/09-20:34:50.321 26dc Creating DB C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Local Storage\leveldb since it was missing. -2026/05/09-20:34:50.334 26dc Reusing MANIFEST C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Local Storage\leveldb/MANIFEST-000001 +2026/05/10-01:32:20.890 744 Reusing MANIFEST C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Local Storage\leveldb/MANIFEST-000001 +2026/05/10-01:32:20.897 744 Recovering log #3 +2026/05/10-01:32:20.898 744 Reusing old log C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Local Storage\leveldb/000003.log diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Network/Network Persistent State b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Network/Network Persistent State index afea2bc..5f6ba7f 100644 --- a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Network/Network Persistent State +++ b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Network/Network Persistent State @@ -1 +1 @@ -{"net":{"http_server_properties":{"servers":[{"anonymization":["GAAAABMAAABodHRwczovL2VjbGlwc2Uub3JnAA==",false,0],"server":"https://www.eclipse.org","supports_spdy":true},{"anonymization":["GAAAABMAAABodHRwczovL2VjbGlwc2Uub3JnAA==",false,0],"server":"https://eclipseide.org","supports_spdy":true},{"anonymization":["GAAAABMAAABodHRwczovL2VjbGlwc2Uub3JnAA==",false,0],"server":"https://membership.eclipse.org","supports_spdy":true}],"version":5},"network_qualities":{"CAASABiAgICA+P////8B":"4G","CAISABiAgICA+P////8B":"4G"}}} \ No newline at end of file +{"net":{"http_server_properties":{"servers":[{"anonymization":["GAAAABMAAABodHRwczovL2VjbGlwc2Uub3JnAA==",false,0],"server":"https://eclipseide.org","supports_spdy":true},{"anonymization":["GAAAABMAAABodHRwczovL2VjbGlwc2Uub3JnAA==",false,0],"server":"https://www.eclipse.org","supports_spdy":true},{"anonymization":["GAAAABMAAABodHRwczovL2VjbGlwc2Uub3JnAA==",false,0],"server":"https://membership.eclipse.org","supports_spdy":true}],"version":5},"network_qualities":{"CAASABiAgICA+P////8B":"4G","CAISABiAgICA+P////8B":"4G"}}} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Network/TransportSecurity b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Network/TransportSecurity index ec578e0..088abd0 100644 --- a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Network/TransportSecurity +++ b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Network/TransportSecurity @@ -1 +1 @@ -{"sts":[{"expiry":1809887691.64094,"host":"Bb6CfwBW9xa4G2DyhI27lujzIm9QMX4njeP0qgg7n+Y=","mode":"force-https","sts_include_subdomains":true,"sts_observed":1778351691.640946},{"expiry":1809905543.007089,"host":"5AUdaPrjbZpycMJSfvB4F6jmmAfkjR133lCOp+js4+E=","mode":"force-https","sts_include_subdomains":true,"sts_observed":1778369543.007089},{"expiry":1809887692.184129,"host":"6Rxq1ifSWlOJhzOYYdWRhfgKRZ4jrGi2e5lJ0HKREHQ=","mode":"force-https","sts_include_subdomains":true,"sts_observed":1778351692.184138}],"version":2} \ No newline at end of file +{"sts":[{"expiry":1809887691.64094,"host":"Bb6CfwBW9xa4G2DyhI27lujzIm9QMX4njeP0qgg7n+Y=","mode":"force-https","sts_include_subdomains":true,"sts_observed":1778351691.640946},{"expiry":1809966643.589161,"host":"5AUdaPrjbZpycMJSfvB4F6jmmAfkjR133lCOp+js4+E=","mode":"force-https","sts_include_subdomains":true,"sts_observed":1778430643.589164},{"expiry":1809887692.184129,"host":"6Rxq1ifSWlOJhzOYYdWRhfgKRZ4jrGi2e5lJ0HKREHQ=","mode":"force-https","sts_include_subdomains":true,"sts_observed":1778351692.184138}],"version":2} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Preferences b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Preferences index fc8b4f3..91da2c1 100644 --- a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Preferences +++ b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Preferences @@ -1 +1 @@ -{"aadc_info":{"age_group":3},"accessibility":{"captions":{"headless_caption_enabled":false}},"account_tracker_service_last_update":"13422825290294571","autocomplete":{"retention_policy_last_version":147},"autofill":{"edge_autofill_advanced_ml_enabled":false,"edge_autofill_purge_low_quality_profiles_by_timeline":false,"last_version_deduped":147},"bookmark":{"storage_computation_last_update":"13422825290289708"},"browser":{"available_dark_theme_options":"All","recent_theme_color_list":[4293914607.0,4293914607.0,4293914607.0,4293914607.0,4293914607.0],"show_toolbar_edge_generic_sidebar_button":false,"theme":{"color_scheme2":2},"user_level_features_context":{}},"browser_content_container_height":206,"browser_content_container_width":602,"browser_content_container_x":0,"browser_content_container_y":0,"commerce_daily_metrics_last_update_time":"13422825290293315","countryid_at_install":18772,"credentials_enable_service":false,"edge":{"bookmarks":{"last_dup_info_record_time":"13422825300291938"},"msa_sso_info":{"allow_for_non_msa_profile":true},"profile_sso_info":{"is_msa_first_profile":true,"msa_sso_algo_state":1},"services":{"signin_scoped_device_id":"96942901-4e8d-4038-904b-06675f324c33"}},"edge_journeys":{"latest_journeys_count":0},"edge_rewards":{"cache_data":"CAA=","coachmark_promotions":{},"hva_promotions":[],"hva_webui_action_status_dict":{},"refresh_status_muted_until":"13423430090265996"},"edge_ux_config":{"assignmentcontext":"","dataversion":"0","experimentvariables":{},"flights":{}},"edge_vpn":{"available":true},"edge_wallet":{"passwords":{"password_lost_report_date":"13422825320278890"}},"enterprise_profile_guid":"c9c307cc-82a2-4be9-89c0-04edbd9e6415","extension":{"installed_extension_count":2},"extensions":{"alerts":{"initialized":true},"chrome_url_overrides":{},"last_chrome_version":"147.0.3912.98","pdf_upsell_triggered":false,"pinned_extension_migration":true,"pinned_extensions":[]},"fsd":{"retention_policy_last_version":147},"gaia_cookie":{"periodic_report_time_2":"13422825290265802"},"intl":{"selected_languages":"en-GB,en,en-US"},"language_dwell_time_average":{"en":6.0},"language_model_counters":{"en":4},"language_usage_count":{"en":1},"media":{"engagement":{"schema_version":5}},"migrated_user_scripts_toggle":true,"muid":{"last_sync":"13422825290293161","values_seen":[]},"optimization_guide":{"hintsfetcher":{"hosts_successfully_fetched":{}},"previously_registered_optimization_types":{"ABOUT_THIS_SITE":true,"AUTOFILL_ACTOR_IFRAME_ORIGIN_ALLOWLIST":true,"GLIC_ACTION_PAGE_BLOCK":true,"HISTORY_CLUSTERS":true,"LOADING_PREDICTOR":true,"MERCHANT_TRUST_SIGNALS_V2":true,"PAGE_ENTITIES":true,"PRICE_TRACKING":true,"SALIENT_IMAGE":true,"SAVED_TAB_GROUP":true,"V8_COMPILE_HINTS":true}},"password_manager":{"account_store_backup_password_cleaning_last_timestamp":"13422825350282160","account_store_migrated_to_os_crypt_async":true,"profile_store_backup_password_cleaning_last_timestamp":"13422825350282713","profile_store_migrated_to_os_crypt_async":true},"personalization_data_consent":{"personalization_in_context_consent_can_prompt":true,"personalization_in_context_count":0},"privacy_sandbox":{"first_party_sets_data_access_allowed_initialized":true},"profile":{"avatar_index":20,"background_password_check":{"check_fri_weight":9,"check_interval":"2592000000000","check_mon_weight":6,"check_sat_weight":6,"check_sun_weight":6,"check_thu_weight":9,"check_tue_weight":9,"check_wed_weight":9,"next_check_time":"13424909115774370"},"content_settings":{"exceptions":{"3pcd_heuristics_grants":{},"abusive_notification_permissions":{},"access_to_get_all_screens_media_in_session":{},"anti_abuse":{},"app_banner":{},"ar":{},"are_suspicious_notifications_allowlisted_by_user":{},"auto_picture_in_picture":{},"auto_select_certificate":{},"automatic_downloads":{},"automatic_fullscreen":{},"autoplay":{},"background_sync":{},"bluetooth_chooser_data":{},"bluetooth_guard":{},"bluetooth_scanning":{},"camera_pan_tilt_zoom":{},"captured_surface_control":{},"clear_browsing_data_cookies_exceptions":{},"client_hints":{},"clipboard":{},"controlled_frame":{},"cookie_controls_metadata":{"file:///*,*":{"last_modified":"13422847998511251","setting":{}},"https://[*.]eclipse.org,*":{"last_modified":"13422843141092695","setting":{}}},"cookies":{},"direct_sockets":{},"direct_sockets_private_network_access":{},"display_media_system_audio":{},"disruptive_notification_permissions":{},"durable_storage":{},"edge_ad_targeting":{},"edge_ad_targeting_data":{},"edge_all_file_read_access":{},"edge_browser_action":{},"edge_sdsm":{},"edge_split_screen":{},"edge_tech_scam_detection":{},"edge_u2f_api_request":{},"edge_user_agent_token":{},"fedcm_idp_registration":{},"fedcm_idp_signin":{},"fedcm_share":{},"file_system_access_chooser_data":{},"file_system_access_extended_permission":{},"file_system_access_restore_permission":{},"file_system_last_picked_directory":{},"file_system_read_guard":{},"file_system_write_guard":{},"formfill_metadata":{},"geolocation":{},"geolocation_with_options":{},"hand_tracking":{},"has_migrated_local_network_access":true,"hid_chooser_data":{},"hid_guard":{},"http_allowed":{},"https_enforced":{},"idle_detection":{},"images":{},"important_site_info":{},"initialized_translations":{},"intent_picker_auto_display":{},"javascript":{},"javascript_jit":{},"javascript_optimizer":{},"keyboard_lock":{},"legacy_cookie_access":{},"legacy_cookie_scope":{},"local_fonts":{},"local_network":{},"local_network_access":{},"loopback_network":{},"media_engagement":{"https://www.eclipse.org:443,*":{"expiration":"13430601356398229","last_modified":"13422825356398233","lifetime":"7776000000000","setting":{"hasHighScore":false,"lastMediaPlaybackTime":0.0,"mediaPlaybacks":0,"visits":1}}},"media_stream_camera":{},"media_stream_mic":{},"midi_sysex":{},"mixed_script":{},"nfc_devices":{},"notification_interactions":{},"notification_permission_review":{},"notifications":{},"ondevice_languages_downloaded":{},"password_protection":{},"payment_handler":{},"permission_actions_history":{},"permission_autoblocking_data":{},"permission_autorevocation_data":{},"pointer_lock":{},"popups":{},"protected_media_identifier":{},"protocol_handler":{},"reduced_accept_language":{},"safe_browsing_url_check_data":{},"secure_network":{},"secure_network_sites":{},"sensors":{},"serial_chooser_data":{},"serial_guard":{},"site_engagement":{"https://www.eclipse.org:443,*":{"last_modified":"13422843141094940","setting":{"lastEngagementTime":1.3422843141094928e+16,"lastShortcutLaunchTime":0.0,"pointsAddedToday":4.5,"rawScore":8.9415}}},"sleeping_tabs":{},"sound":{},"speaker_selection":{},"ssl_cert_decisions":{},"storage_access":{},"storage_access_header_origin_trial":{},"subresource_filter":{},"subresource_filter_data":{},"suspicious_notification_ids":{},"suspicious_notification_show_original":{},"top_level_storage_access":{},"trackers":{},"trackers_data":{},"tracking_org_exceptions":{},"tracking_org_relationships":{},"typosquatting":{},"unused_site_permissions":{},"usb_chooser_data":{},"usb_guard":{},"vr":{},"web_app_installation":{},"webid_api":{},"webid_auto_reauthn":{},"window_placement":{}},"pref_version":1},"created_by_version":"147.0.3912.98","creation_time":"13422825290227113","default_content_setting_values":{"has_migrated_local_network_access":true},"edge_password_is_using_new_login_db_path":false,"edge_password_login_db_path_flip_flop_count":0,"edge_profile_id":"a229742e-1a76-40e7-b7b9-b0cbc2f225f1","edge_user_with_non_zero_passwords":false,"has_seen_signin_fre":false,"is_relative_to_aad":false,"last_engagement_time":"13422843141094928","last_time_obsolete_http_credentials_removed":1778351750.282291,"last_time_password_store_metrics_reported":1778351720.277159,"managed_user_id":"","name":"Profile 1","network_pbs":{},"observed_session_time":{"feedback_rating_in_product_help_observed_session_time_key_147.0.3912.98":6.0},"password_hash_data_list":[],"signin_fre_seen_time":"13422825290260630","were_old_google_logins_removed":true},"reset_prepopulated_engines":false,"safety_hub":{"unused_site_permissions_revocation":{"migration_completed":true}},"saved_tab_groups":{"did_enable_shared_tab_groups_in_last_session":false,"specifics_to_data_migration":true},"sessions":{"event_log":[{"crashed":false,"time":"13422825290268704","type":0},{"did_schedule_command":false,"first_session_service":true,"tab_count":1,"time":"13422825356403533","type":2,"window_count":1},{"crashed":false,"time":"13422843140823994","type":0},{"did_schedule_command":false,"first_session_service":true,"tab_count":3,"time":"13422843168919448","type":2,"window_count":3}],"session_data_status":1},"signin":{"accounts_metadata_dict":{},"allowed":true},"spellcheck":{"dictionaries":["en-GB"]},"syncing_theme_prefs_migrated_to_non_syncing":true,"tab_search":{"pinned_to_tabstrip":false,"pinned_to_tabstrip_migration_complete":true},"total_passwords_available_for_account":0,"total_passwords_available_for_profile":0,"translate_site_blacklist":[],"translate_site_blocklist_with_time":{},"typosquatting":{"allowlist_migration_done":true},"user_experience_metrics":{"personalization_data_consent_enabled_last_known_value":false},"webrtc":{"udp_port_range":"0-0"}} \ No newline at end of file +{"aadc_info":{"age_group":3},"accessibility":{"captions":{"headless_caption_enabled":false}},"account_tracker_service_last_update":"13422911690305312","autocomplete":{"retention_policy_last_version":147},"autofill":{"edge_autofill_advanced_ml_enabled":false,"edge_autofill_purge_low_quality_profiles_by_timeline":false,"last_version_deduped":147},"bookmark":{"storage_computation_last_update":"13422911690303915"},"browser":{"available_dark_theme_options":"All","recent_theme_color_list":[4293914607.0,4293914607.0,4293914607.0,4293914607.0,4293914607.0],"show_toolbar_edge_generic_sidebar_button":false,"theme":{"color_scheme2":2},"user_level_features_context":{}},"browser_content_container_height":67,"browser_content_container_width":183,"browser_content_container_x":0,"browser_content_container_y":0,"commerce_daily_metrics_last_update_time":"13422911690304426","countryid_at_install":18772,"credentials_enable_service":false,"edge":{"bookmarks":{"last_dup_info_record_time":"13422825300291938"},"msa_sso_info":{"allow_for_non_msa_profile":true},"profile_sso_info":{"is_msa_first_profile":true,"msa_sso_algo_state":1},"services":{"signin_scoped_device_id":"3165f288-04ad-476d-92d1-abea48bbc919"}},"edge_journeys":{"latest_journeys_count":0},"edge_rewards":{"cache_data":"CAA=","coachmark_promotions":{},"hva_promotions":[],"hva_webui_action_status_dict":{},"refresh_status_muted_until":"13423430090265996"},"edge_ux_config":{"assignmentcontext":"","dataversion":"0","experimentvariables":{},"flights":{}},"edge_vpn":{"available":true},"edge_wallet":{"passwords":{"password_lost_report_date":"13422825320278890"}},"enterprise_profile_guid":"c9c307cc-82a2-4be9-89c0-04edbd9e6415","extension":{"installed_extension_count":2},"extensions":{"alerts":{"initialized":true},"chrome_url_overrides":{},"last_chrome_version":"147.0.3912.98","pdf_upsell_triggered":false,"pinned_extension_migration":true,"pinned_extensions":[]},"fsd":{"retention_policy_last_version":147},"gaia_cookie":{"periodic_report_time_2":"13422911690272491"},"intl":{"selected_languages":"en-GB,en,en-US"},"language_dwell_time_average":{"en":19375.166666666668,"it":3.0},"language_model_counters":{"en":11,"it":1},"language_usage_count":{"en":6,"it":1},"media":{"engagement":{"schema_version":5}},"migrated_user_scripts_toggle":true,"muid":{"last_sync":"13422911690304680","values_seen":[]},"optimization_guide":{"hintsfetcher":{"hosts_successfully_fetched":{}},"previously_registered_optimization_types":{"ABOUT_THIS_SITE":true,"AUTOFILL_ACTOR_IFRAME_ORIGIN_ALLOWLIST":true,"GLIC_ACTION_PAGE_BLOCK":true,"HISTORY_CLUSTERS":true,"LOADING_PREDICTOR":true,"MERCHANT_TRUST_SIGNALS_V2":true,"PAGE_ENTITIES":true,"PRICE_TRACKING":true,"SALIENT_IMAGE":true,"SAVED_TAB_GROUP":true,"V8_COMPILE_HINTS":true}},"password_manager":{"account_store_backup_password_cleaning_last_timestamp":"13422825350282160","account_store_migrated_to_os_crypt_async":true,"profile_store_backup_password_cleaning_last_timestamp":"13422825350282713","profile_store_migrated_to_os_crypt_async":true},"personalization_data_consent":{"personalization_in_context_consent_can_prompt":true,"personalization_in_context_count":0},"privacy_sandbox":{"first_party_sets_data_access_allowed_initialized":true},"profile":{"avatar_index":20,"background_password_check":{"check_fri_weight":9,"check_interval":"2592000000000","check_mon_weight":6,"check_sat_weight":6,"check_sun_weight":6,"check_thu_weight":9,"check_tue_weight":9,"check_wed_weight":9,"next_check_time":"13424909115774370"},"content_settings":{"exceptions":{"3pcd_heuristics_grants":{},"abusive_notification_permissions":{},"access_to_get_all_screens_media_in_session":{},"anti_abuse":{},"app_banner":{},"ar":{},"are_suspicious_notifications_allowlisted_by_user":{},"auto_picture_in_picture":{},"auto_select_certificate":{},"automatic_downloads":{},"automatic_fullscreen":{},"autoplay":{},"background_sync":{},"bluetooth_chooser_data":{},"bluetooth_guard":{},"bluetooth_scanning":{},"camera_pan_tilt_zoom":{},"captured_surface_control":{},"clear_browsing_data_cookies_exceptions":{},"client_hints":{},"clipboard":{},"controlled_frame":{},"cookie_controls_metadata":{"file:///*,*":{"last_modified":"13422987294856216","setting":{}},"https://[*.]eclipse.org,*":{"last_modified":"13422904240321357","setting":{}}},"cookies":{},"direct_sockets":{},"direct_sockets_private_network_access":{},"display_media_system_audio":{},"disruptive_notification_permissions":{},"durable_storage":{},"edge_ad_targeting":{},"edge_ad_targeting_data":{},"edge_all_file_read_access":{},"edge_browser_action":{},"edge_sdsm":{},"edge_split_screen":{},"edge_tech_scam_detection":{},"edge_u2f_api_request":{},"edge_user_agent_token":{},"fedcm_idp_registration":{},"fedcm_idp_signin":{},"fedcm_share":{},"file_system_access_chooser_data":{},"file_system_access_extended_permission":{},"file_system_access_restore_permission":{},"file_system_last_picked_directory":{},"file_system_read_guard":{},"file_system_write_guard":{},"formfill_metadata":{},"geolocation":{},"geolocation_with_options":{},"hand_tracking":{},"has_migrated_local_network_access":true,"hid_chooser_data":{},"hid_guard":{},"http_allowed":{},"https_enforced":{},"idle_detection":{},"images":{},"important_site_info":{},"initialized_translations":{},"intent_picker_auto_display":{},"javascript":{},"javascript_jit":{},"javascript_optimizer":{},"keyboard_lock":{},"legacy_cookie_access":{},"legacy_cookie_scope":{},"local_fonts":{},"local_network":{},"local_network_access":{},"loopback_network":{},"media_engagement":{"https://www.eclipse.org:443,*":{"expiration":"13430677242302836","last_modified":"13422901242302841","lifetime":"7776000000000","setting":{"hasHighScore":false,"lastMediaPlaybackTime":0.0,"mediaPlaybacks":0,"visits":3}}},"media_stream_camera":{},"media_stream_mic":{},"midi_sysex":{},"mixed_script":{},"nfc_devices":{},"notification_interactions":{},"notification_permission_review":{},"notifications":{},"ondevice_languages_downloaded":{},"password_protection":{},"payment_handler":{},"permission_actions_history":{},"permission_autoblocking_data":{},"permission_autorevocation_data":{},"pointer_lock":{},"popups":{},"protected_media_identifier":{},"protocol_handler":{},"reduced_accept_language":{},"safe_browsing_url_check_data":{},"secure_network":{},"secure_network_sites":{},"sensors":{},"serial_chooser_data":{},"serial_guard":{},"site_engagement":{"https://www.eclipse.org:443,*":{"last_modified":"13422985902453009","setting":{"lastEngagementTime":1.342295710245282e+16,"lastShortcutLaunchTime":0.0,"pointsAddedToday":0.0,"rawScore":11.7105321135}}},"sleeping_tabs":{},"sound":{},"speaker_selection":{},"ssl_cert_decisions":{},"storage_access":{},"storage_access_header_origin_trial":{},"subresource_filter":{},"subresource_filter_data":{},"suspicious_notification_ids":{},"suspicious_notification_show_original":{},"top_level_storage_access":{},"trackers":{},"trackers_data":{},"tracking_org_exceptions":{},"tracking_org_relationships":{},"typosquatting":{},"unused_site_permissions":{},"usb_chooser_data":{},"usb_guard":{},"vr":{},"web_app_installation":{},"webid_api":{},"webid_auto_reauthn":{},"window_placement":{}},"pref_version":1},"created_by_version":"147.0.3912.98","creation_time":"13422825290227113","default_content_setting_values":{"has_migrated_local_network_access":true},"edge_password_is_using_new_login_db_path":false,"edge_password_login_db_path_flip_flop_count":0,"edge_profile_id":"a229742e-1a76-40e7-b7b9-b0cbc2f225f1","edge_user_with_non_zero_passwords":false,"exit_type":"Normal","has_seen_signin_fre":false,"is_relative_to_aad":false,"last_engagement_time":"13422957102452820","last_time_obsolete_http_credentials_removed":1778351750.282291,"last_time_password_store_metrics_reported":1778351720.277159,"managed_user_id":"","name":"Profile 1","network_pbs":{},"observed_session_time":{"feedback_rating_in_product_help_observed_session_time_key_147.0.3912.98":7.0},"password_hash_data_list":[],"signin_fre_seen_time":"13422825290260630","were_old_google_logins_removed":true},"reset_prepopulated_engines":false,"safety_hub":{"unused_site_permissions_revocation":{"migration_completed":true}},"saved_tab_groups":{"did_enable_shared_tab_groups_in_last_session":false,"specifics_to_data_migration":true},"sessions":{"event_log":[{"crashed":false,"time":"13422825290268704","type":0},{"did_schedule_command":false,"first_session_service":true,"tab_count":1,"time":"13422825356403533","type":2,"window_count":1},{"crashed":false,"time":"13422843140823994","type":0},{"did_schedule_command":false,"first_session_service":true,"tab_count":3,"time":"13422843168919448","type":2,"window_count":3},{"crashed":false,"time":"13422904239947130","type":0},{"did_schedule_command":false,"first_session_service":true,"tab_count":3,"time":"13422905046795006","type":2,"window_count":3}],"session_data_status":1},"signin":{"accounts_metadata_dict":{},"allowed":true},"spellcheck":{"dictionaries":["en-GB"]},"syncing_theme_prefs_migrated_to_non_syncing":true,"tab_search":{"pinned_to_tabstrip":false,"pinned_to_tabstrip_migration_complete":true},"total_passwords_available_for_account":0,"total_passwords_available_for_profile":0,"translate_site_blacklist":[],"translate_site_blocklist_with_time":{},"typosquatting":{"allowlist_migration_done":true},"user_experience_metrics":{"personalization_data_consent_enabled_last_known_value":false},"webrtc":{"udp_port_range":"0-0"}} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Secure Preferences b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Secure Preferences index 394374b..087bf40 100644 --- a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Secure Preferences +++ b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Secure Preferences @@ -1 +1 @@ -{"extensions":{"settings":{"dgiklkfkllikcanfonkcabmbdfmgleag":{"account_extension_type":0,"active_permissions":{"api":[],"explicit_host":[],"manifest_permissions":[],"scriptable_host":[]},"commands":{},"content_settings":[],"creation_flags":1,"disable_reasons":[],"events":[],"first_install_time":"13422825290276856","from_webstore":false,"incognito_content_settings":[],"incognito_preferences":{},"last_update_time":"13422825290276856","location":5,"manifest":{"content_capabilities":{"include_globs":["https://*excel.officeapps.live.com/*","https://*onenote.officeapps.live.com/*","https://*powerpoint.officeapps.live.com/*","https://*word-edit.officeapps.live.com/*","https://*excel.officeapps.live.com.mcas.ms/*","https://*onenote.officeapps.live.com.mcas.ms/*","https://*word-edit.officeapps.live.com.mcas.ms/*","https://*excel.partner.officewebapps.cn/*","https://*onenote.partner.officewebapps.cn/*","https://*powerpoint.partner.officewebapps.cn/*","https://*word-edit.partner.officewebapps.cn/*","https://*excel.gov.online.office365.us/*","https://*onenote.gov.online.office365.us/*","https://*powerpoint.gov.online.office365.us/*","https://*word-edit.gov.online.office365.us/*","https://*excel.dod.online.office365.us/*","https://*onenote.dod.online.office365.us/*","https://*powerpoint.dod.online.office365.us/*","https://*word-edit.dod.online.office365.us/*","https://*visio.partner.officewebapps.cn/*","https://*visio.gov.online.office365.us/*","https://*visio.dod.online.office365.us/*"],"matches":["https://*.officeapps.live.com/*","https://*.officeapps.live.com.mcas.ms/*","https://*.partner.officewebapps.cn/*","https://*.gov.online.office365.us/*","https://*.dod.online.office365.us/*","https://*.app.whiteboard.microsoft.com/*","https://*.whiteboard.office.com/*","https://*.app.int.whiteboard.microsoft.com/*","https://*.whiteboard.office365.us/*","https://*.dev.whiteboard.microsoft.com/*"],"permissions":["clipboardRead","clipboardWrite"]},"default_locale":"en","description":"This extension grants Microsoft web sites permission to read and write from the clipboard.","key":"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCz4t/X7GeuP6GBpjmxndrjtzF//4CWeHlC68rkoV7hP3h5Ka6eX7ZMNlYJkSjmB5iRmPHO5kR1y7rGY8JXnRPDQh/CQNLVA7OsKeV6w+UO+vx8KGI+TrTAhzH8YGcMIsxsUjxtC4cBmprja+xDr0zVp2EMgqHu+GBKgwSRHTkDuwIDAQAB","manifest_version":2,"minimum_chrome_version":"77","name":"Microsoft Clipboard Extension","version":"1.0"},"path":"C:\\Program Files (x86)\\Microsoft\\EdgeWebView\\Application\\147.0.3912.98\\resources\\edge_clipboard","preferences":{},"regular_only_preferences":{},"was_installed_by_default":false,"was_installed_by_oem":false},"mhjfbmdgcfjbbpaeojofohoefgiehjai":{"account_extension_type":0,"active_permissions":{"api":["contentSettings","fileSystem","fileSystem.write","metricsPrivate","tabs","resourcesPrivate","pdfViewerPrivate","fileSystem.readFullPath","errorReporting","edgeLearningToolsPrivate","fileSystem.getCurrentEntry","edgePdfPrivate","edgeCertVerifierPrivate"],"explicit_host":["edge://resources/*","edge://webui-test/*"],"manifest_permissions":[],"scriptable_host":[]},"commands":{},"content_settings":[],"creation_flags":1,"disable_reasons":[],"events":[],"first_install_time":"13422825290276320","from_webstore":false,"incognito_content_settings":[],"incognito_preferences":{},"last_update_time":"13422825290276320","location":5,"manifest":{"content_security_policy":"script-src 'self' 'wasm-eval' blob: filesystem: chrome://resources chrome://webui-test; object-src * blob: externalfile: file: filesystem: data:; trusted-types edge-internal fast-html pdf-url edge-pdf-static-policy;","description":"","incognito":"split","key":"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDN6hM0rsDYGbzQPQfOygqlRtQgKUXMfnSjhIBL7LnReAVBEd7ZmKtyN2qmSasMl4HZpMhVe2rPWVVwBDl6iyNE/Kok6E6v6V3vCLGsOpQAuuNVye/3QxzIldzG/jQAdWZiyXReRVapOhZtLjGfywCvlWq7Sl/e3sbc0vWybSDI2QIDAQAB","manifest_version":2,"mime_types":["application/pdf"],"mime_types_handler":"edge_pdf/index.html","name":"Microsoft Edge PDF Viewer","offline_enabled":true,"permissions":["errorReporting","chrome://resources/","chrome://webui-test/","contentSettings","metricsPrivate","edgeCertVerifierPrivate","edgeLearningToolsPrivate","edgePdfPrivate","pdfViewerPrivate","resourcesPrivate","tabs",{"fileSystem":["write","readFullPath","getCurrentEntry"]}],"version":"1","web_accessible_resources":["pdf_embedder.css"]},"path":"C:\\Program Files (x86)\\Microsoft\\EdgeWebView\\Application\\147.0.3912.98\\resources\\edge_pdf","preferences":{},"regular_only_preferences":{},"was_installed_by_default":false,"was_installed_by_oem":false}}},"protection":{"macs":{"browser":{"show_home_button":"EF8E04BCF6F7C232231E69DAE481170653D82ED686DDBF2991FEFD946EB7522E","show_home_button_encrypted_hash":"djEwLi+jbhkvR/7o+ozNw32Xi/C4uDywQB2+IprpNRZteJQknkgDJXj+HEABz1/1O4TcJmHT2iqhhtXIr6UX"},"default_search_provider_data":{"template_url_data":"FCFE985DC4470CDE6D77C27E055CB0622C9CC7AD6BC6B9E1847519F7B8DEE144","template_url_data_encrypted_hash":"djEwI7hlU3MO78qIPqf6/bXTKV9KIUA5O1GBadCdtgF92Adm2+zbt3+BMKzr550jJQ9hiAi4334BKSc1ZOkM"},"edge":{"services":{"account_id":"CEF02DAD6F29EDA114575937C8F508621AE39F5C2738FAEDF89FCBB71E1C1BA3","account_id_encrypted_hash":"djEwWLxOYQFfq9Lkvsy5T9zhSHv/mYFO8gwvm2GJM7y5F1a7tV2urfEcan8wZ3UkNRTAiS1kAI/Hz6bbtakg","last_username":"103B84FBAB0098687AD51A1C08493EFDAEB5F7EF6D86BBE2F39A61127527FBB7","last_username_encrypted_hash":"djEwmLGVmaQs5CfY6R3LQle4n2ZbdTrjsxMMClWoQtJv07LfiJWvateA/dm/VAM1KJTLrlvvWsDB9zp5I91H"}},"enterprise_signin":{"policy_recovery_token":"CD42B0D9C6B42377D8101DB574DD3166F3A9E94F1DB639394916929D5EF5DAC2","policy_recovery_token_encrypted_hash":"djEwj5mQhQZF5IJ0r0Ih577V7I9NYeR1KlMIZSogM5rI0Jlbrosy9YDX8NK0OE0LYFr3XBhguxwdXdqvyUWn"},"extensions":{"install":{"initiallist":"3E78B078F98B045DFE906AB9531C92D6208035B4C12DBA309EFB5E15958EB4A5","initiallist_encrypted_hash":"djEwZ2yNjJz9d+JMCs1vCLJObwMKPyoddma9ibLj3/xMs9uT8U6v5Aptlmq8lgjCXVCZSo/mayur5QsYwpdY","initialprovidername":"40E3B3529940888791F691E760B354D1975C4DE1C69D4F79D038D1E6E57213DF","initialprovidername_encrypted_hash":"djEwyD3gyoMYOcwgvfPMX5EhF0FaFQo8ZDDQSQ3AaNOPjfIKs+0G771q83p57UY03l8IvCHO1uJOj7K2/GB+"},"settings":{"dgiklkfkllikcanfonkcabmbdfmgleag":"DA5FA348ACCFF784D34BB92F5675EE2FB160B53982751D3826CC007363E8284B","mhjfbmdgcfjbbpaeojofohoefgiehjai":"E5EC068D6C43F3F738FE1B38C3A7D681AD40CE86A10355B79FC06F895BAC9828"},"settings_encrypted_hash":{"dgiklkfkllikcanfonkcabmbdfmgleag":"djEwiGy7a51yq+hD6c6xadWuwx9rELTr2r9w4BnfUyZYmkC0K54gdKK9TsKXHhXJTf1f8fPerufA2SY9Uuin","mhjfbmdgcfjbbpaeojofohoefgiehjai":"djEwnOqCu5FwMC4huWa7aCYIaWMhIR7ZG+GED7Af8M7xo8psJ8YML5SFTyMqSvwZGpAsLLx3O0XJMgmqDO5W"},"ui":{"developer_mode":"2DBEBCCD41AA2009A8A787B8BD6D40749D4BAE6F5B4C817F2185B896E17E2F88","developer_mode_encrypted_hash":"djEw/sWtRidEbHytQ7wib8ET8T5HJvgxvYZ254rooDVgUQoAfY65Su8l6exqTUKPndvaHgSSl3x9SoFoc4hl"}},"google":{"services":{"last_signed_in_username":"072FB2F17167EF75B4CFC99246F44C08AC2BBEF6A9E38CE03B2024A42C23FE84","last_signed_in_username_encrypted_hash":"djEwWefUrCH+g33ZkbLBD3YB6MSEUyZcddEtmpm2kHiPhb3jd6AHphlGEIY/5hIPqVgv4VAbz2kQeVde5o9Y"}},"homepage":"78069B504433DDA1B1E463C057E727B790FEF1FCC64CAB5672F563FF4BE366FF","homepage_encrypted_hash":"djEw+zx3WX3fhEL0sAn1QUIgryJYjb5vvO8G+DxTyPfhf0msBYxvtajcosVHr3jYOczT8BURr7vLCstbEIoJ","homepage_is_newtabpage":"F733D8383D60B3033BE28BA305EECEDE724D34C8FD94B30025F423B51393A590","homepage_is_newtabpage_encrypted_hash":"djEwI105gH2bzkFk3eDA/hcH3Q53O+rFhV01EDwTUi5SbF2BvbUikM35RO799ft0KNrVCUItS53FfXJUtD6p","media":{"cdm":{"origin_data":"D37EC83B68B1EF5650F106F4A24081A49B4DF9F5678E7EC6A85E161A98A08E4B","origin_data_encrypted_hash":"djEwerkxbZSmyLYMn4VGTu6YkHr3p3WeIfGI/u3/DGtOsjoJoShTRReQzdLL6MW0yeFVkxZjr+Z21lzuLyBn"},"storage_id_salt":"B2E9290C6F6859BEB974309F06D9E137958B4AE07302B17AAE243C417F7EA569","storage_id_salt_encrypted_hash":"djEwsG2oEG4aehwiUI0vxi86orz5tRl9Tws/x57pqiO9pbFBKVqisJmtD/TnYqB82pBvxjlbD9M7JnTZgAGQ"},"pinned_tabs":"6EE16DC2F5850603ED8BDEA03AEFF18B572EE9FFFBC60EA1A9EED8FA68C0F5DB","pinned_tabs_encrypted_hash":"djEwQeLfu6gvGd74o46JhQnIlIT/pjcNKzwVArfmxhRBZHv/tumbr2YI3kN4BJ0YS5BnONBkbpXPfjQ4zI6J","prefs":{"preference_reset_time":"15C19F277D4917DCF71B31F461B2D0C8DBB3B848428CA9BC3C5C6BC48574AC87","preference_reset_time_encrypted_hash":"djEwRqWrKUaHjYoL3TwBx0Ur72NIdG9lp1O+KeTAUJaoxvIwa3A7xMpLkYZU8aeaOufvofRGPrjzYfdfGRJz"},"safebrowsing":{"incidents_sent":"939F8A46FD612B2F11302FE860AA28AC2BB97403B61A579D55FF125171603A19","incidents_sent_encrypted_hash":"djEw9lRYbXo1ECRABI4SdTISNQLkJLzqgZR34UcQyUTm0hyg9u/9NcF8zCTFqJsv8H0pT3fD3Ou4nMIrEvgr"},"schedule_to_flush_to_disk":"E338965BF58D80E500083209C29838A87B0CEC393547ED6D5B328F04290C3BBB","schedule_to_flush_to_disk_encrypted_hash":"djEwLapIDql5tB5wghUkLg0pFu098eDhsxn2TfHMCLUpkg66lWMTpT4tgFOQDuabg33qRZR0d56CXwgoCkuk","search_provider_overrides":"F89D61352813CEF1214763E778885A6264DB78F18556CF125DB7E7269A975DEA","search_provider_overrides_encrypted_hash":"djEwxDhudWw5e2plQnxwmk2EAJq23SPWpsYfyEnO/pQc7kX4AxIKGX/A1PxtbCLiXj3lgp+c8MzbWlzB5rQ/","session":{"restore_on_startup":"01D0DC40F4692ABDB2D5ACF617641F9F792DFAF368CDC870D173736D7C3A2DF1","restore_on_startup_encrypted_hash":"djEwjvPMfR1nxvFXpZ3xKnnS7khth7BvuAZvn3YL2SjvCj4XbbnCZ1yqCW2pleFGluYrEwTEXssdQzZiKiIx","startup_urls":"D1197E0D92DA48044470C7DAA2988BB7F626A57B23FF417848ECDAC833189798","startup_urls_encrypted_hash":"djEwTZ/PJ9et6/eEs3QAbr5DACWzSLTuD4Y6Yd+b5sehto7iQd43adMRyMmMmM9Cr6qfXoaEX9Ngk+cZsiax"}},"super_mac":"66A83DD995FAF40E0DE573A096B0053EC6C1F58E05400E97BB80D443C9DDB147"},"schedule_to_flush_to_disk":"13422843140836810"} \ No newline at end of file +{"extensions":{"settings":{"dgiklkfkllikcanfonkcabmbdfmgleag":{"account_extension_type":0,"active_permissions":{"api":[],"explicit_host":[],"manifest_permissions":[],"scriptable_host":[]},"commands":{},"content_settings":[],"creation_flags":1,"disable_reasons":[],"events":[],"first_install_time":"13422825290276856","from_webstore":false,"incognito_content_settings":[],"incognito_preferences":{},"last_update_time":"13422825290276856","location":5,"manifest":{"content_capabilities":{"include_globs":["https://*excel.officeapps.live.com/*","https://*onenote.officeapps.live.com/*","https://*powerpoint.officeapps.live.com/*","https://*word-edit.officeapps.live.com/*","https://*excel.officeapps.live.com.mcas.ms/*","https://*onenote.officeapps.live.com.mcas.ms/*","https://*word-edit.officeapps.live.com.mcas.ms/*","https://*excel.partner.officewebapps.cn/*","https://*onenote.partner.officewebapps.cn/*","https://*powerpoint.partner.officewebapps.cn/*","https://*word-edit.partner.officewebapps.cn/*","https://*excel.gov.online.office365.us/*","https://*onenote.gov.online.office365.us/*","https://*powerpoint.gov.online.office365.us/*","https://*word-edit.gov.online.office365.us/*","https://*excel.dod.online.office365.us/*","https://*onenote.dod.online.office365.us/*","https://*powerpoint.dod.online.office365.us/*","https://*word-edit.dod.online.office365.us/*","https://*visio.partner.officewebapps.cn/*","https://*visio.gov.online.office365.us/*","https://*visio.dod.online.office365.us/*"],"matches":["https://*.officeapps.live.com/*","https://*.officeapps.live.com.mcas.ms/*","https://*.partner.officewebapps.cn/*","https://*.gov.online.office365.us/*","https://*.dod.online.office365.us/*","https://*.app.whiteboard.microsoft.com/*","https://*.whiteboard.office.com/*","https://*.app.int.whiteboard.microsoft.com/*","https://*.whiteboard.office365.us/*","https://*.dev.whiteboard.microsoft.com/*"],"permissions":["clipboardRead","clipboardWrite"]},"default_locale":"en","description":"This extension grants Microsoft web sites permission to read and write from the clipboard.","key":"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCz4t/X7GeuP6GBpjmxndrjtzF//4CWeHlC68rkoV7hP3h5Ka6eX7ZMNlYJkSjmB5iRmPHO5kR1y7rGY8JXnRPDQh/CQNLVA7OsKeV6w+UO+vx8KGI+TrTAhzH8YGcMIsxsUjxtC4cBmprja+xDr0zVp2EMgqHu+GBKgwSRHTkDuwIDAQAB","manifest_version":2,"minimum_chrome_version":"77","name":"Microsoft Clipboard Extension","version":"1.0"},"path":"C:\\Program Files (x86)\\Microsoft\\EdgeWebView\\Application\\147.0.3912.98\\resources\\edge_clipboard","preferences":{},"regular_only_preferences":{},"was_installed_by_default":false,"was_installed_by_oem":false},"mhjfbmdgcfjbbpaeojofohoefgiehjai":{"account_extension_type":0,"active_permissions":{"api":["contentSettings","fileSystem","fileSystem.write","metricsPrivate","tabs","resourcesPrivate","pdfViewerPrivate","fileSystem.readFullPath","errorReporting","edgeLearningToolsPrivate","fileSystem.getCurrentEntry","edgePdfPrivate","edgeCertVerifierPrivate"],"explicit_host":["edge://resources/*","edge://webui-test/*"],"manifest_permissions":[],"scriptable_host":[]},"commands":{},"content_settings":[],"creation_flags":1,"disable_reasons":[],"events":[],"first_install_time":"13422825290276320","from_webstore":false,"incognito_content_settings":[],"incognito_preferences":{},"last_update_time":"13422825290276320","location":5,"manifest":{"content_security_policy":"script-src 'self' 'wasm-eval' blob: filesystem: chrome://resources chrome://webui-test; object-src * blob: externalfile: file: filesystem: data:; trusted-types edge-internal fast-html pdf-url edge-pdf-static-policy;","description":"","incognito":"split","key":"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDN6hM0rsDYGbzQPQfOygqlRtQgKUXMfnSjhIBL7LnReAVBEd7ZmKtyN2qmSasMl4HZpMhVe2rPWVVwBDl6iyNE/Kok6E6v6V3vCLGsOpQAuuNVye/3QxzIldzG/jQAdWZiyXReRVapOhZtLjGfywCvlWq7Sl/e3sbc0vWybSDI2QIDAQAB","manifest_version":2,"mime_types":["application/pdf"],"mime_types_handler":"edge_pdf/index.html","name":"Microsoft Edge PDF Viewer","offline_enabled":true,"permissions":["errorReporting","chrome://resources/","chrome://webui-test/","contentSettings","metricsPrivate","edgeCertVerifierPrivate","edgeLearningToolsPrivate","edgePdfPrivate","pdfViewerPrivate","resourcesPrivate","tabs",{"fileSystem":["write","readFullPath","getCurrentEntry"]}],"version":"1","web_accessible_resources":["pdf_embedder.css"]},"path":"C:\\Program Files (x86)\\Microsoft\\EdgeWebView\\Application\\147.0.3912.98\\resources\\edge_pdf","preferences":{},"regular_only_preferences":{},"was_installed_by_default":false,"was_installed_by_oem":false}}},"protection":{"macs":{"browser":{"show_home_button":"EF8E04BCF6F7C232231E69DAE481170653D82ED686DDBF2991FEFD946EB7522E","show_home_button_encrypted_hash":"djEwES0pBeA6lQPz4lNbUECw65WK0BMloADMhqKdbIa/fznAey0Kth2p+EaK0w0axHpT1rPQWjXtSIWwMnDd"},"default_search_provider_data":{"template_url_data":"FCFE985DC4470CDE6D77C27E055CB0622C9CC7AD6BC6B9E1847519F7B8DEE144","template_url_data_encrypted_hash":"djEwk7S2do81tv8/2L6h4wuBu5o+3bXC9QwSFWqgY8ZvxW3VlZk6bQSdj4stYDJuvNmG603nhlQBrz8+3XXR"},"edge":{"services":{"account_id":"CEF02DAD6F29EDA114575937C8F508621AE39F5C2738FAEDF89FCBB71E1C1BA3","account_id_encrypted_hash":"djEw2BOo4Kw3h6SJFCTtF0wwbvdgCwecr5lrbyHqTlUCCvoLFYf6+cesaU/wjwuSjr6tKBKYcYf/HW1ENfxj","last_username":"103B84FBAB0098687AD51A1C08493EFDAEB5F7EF6D86BBE2F39A61127527FBB7","last_username_encrypted_hash":"djEwKmCwiq8DXtbQFFTv/utTpP2GW1/+skfUThLjoikfLfFBm09xvIDwkygInruh6ATopBOkLxYbWWUiNDwf"}},"enterprise_signin":{"policy_recovery_token":"CD42B0D9C6B42377D8101DB574DD3166F3A9E94F1DB639394916929D5EF5DAC2","policy_recovery_token_encrypted_hash":"djEwNrdta60Rpxq10CEnFFeNfJ4R2AD+l08FSV/8ytPRhcvQ/t21kvZS+qENMRWWYBSEKlr60wqSuNHExTq+"},"extensions":{"install":{"initiallist":"3E78B078F98B045DFE906AB9531C92D6208035B4C12DBA309EFB5E15958EB4A5","initiallist_encrypted_hash":"djEwBeIvUIayxyzG3p8lnU9BbL4Pt/TrkTABdvc6frgnnW+bCnGGlZVVpvlW8Zwd8a0hjCHogn3wLlOyizRF","initialprovidername":"40E3B3529940888791F691E760B354D1975C4DE1C69D4F79D038D1E6E57213DF","initialprovidername_encrypted_hash":"djEw/RZ00y2uQPAXebeeNPsDwujFQLhfALCaJ2CzqAW4i8rymg/7qRLG09FxhrHmilW2jzcoWUFfgc6Wdl2M"},"settings":{"dgiklkfkllikcanfonkcabmbdfmgleag":"DA5FA348ACCFF784D34BB92F5675EE2FB160B53982751D3826CC007363E8284B","mhjfbmdgcfjbbpaeojofohoefgiehjai":"E5EC068D6C43F3F738FE1B38C3A7D681AD40CE86A10355B79FC06F895BAC9828"},"settings_encrypted_hash":{"dgiklkfkllikcanfonkcabmbdfmgleag":"djEwW/W9yUl95wR3OGLokJJFO8ZdrKccBm/uMXPM/U7lceKoZfJlKHCRfpUzZ5FDiAFsm1F9er0eUXOHd+zi","mhjfbmdgcfjbbpaeojofohoefgiehjai":"djEwnyvcsab2OJaIfppRjaBRM84Crj6e0RZtWqcCGW5W4ZPKNYoozij1UymuHY09paRQQikacNHSTx4jg5jU"},"ui":{"developer_mode":"2DBEBCCD41AA2009A8A787B8BD6D40749D4BAE6F5B4C817F2185B896E17E2F88","developer_mode_encrypted_hash":"djEwc7uGx7laHV7W1VeuE37LFm0BkWXFziluUFpLwYjlZ8G85nhu/97IJuOPM0ETB3TSnKCZruEBTDB0hkkk"}},"google":{"services":{"last_signed_in_username":"072FB2F17167EF75B4CFC99246F44C08AC2BBEF6A9E38CE03B2024A42C23FE84","last_signed_in_username_encrypted_hash":"djEwRlCeGX7jE9sPz2CVaSwjnPIfAyWRVM+Tz88xtVtDNXr2uYBtVWH9m0eOcuSNMeDMrb5HbPZ/2JhWXdfI"}},"homepage":"78069B504433DDA1B1E463C057E727B790FEF1FCC64CAB5672F563FF4BE366FF","homepage_encrypted_hash":"djEwrJlCKVFmpUuUVIESwO6doE6i65oaHdi9zv8KtqJx0U8+SuFQuNzz++EspSwwbvs9cmAwYiPdOJcPRWrJ","homepage_is_newtabpage":"F733D8383D60B3033BE28BA305EECEDE724D34C8FD94B30025F423B51393A590","homepage_is_newtabpage_encrypted_hash":"djEwJpUKpUaA0PwCbjzUz6Kq3rMtRB8wstyYgaiblVPMuSxRIfrR/GFrOOafP4ZJdKxPP2LTRIgnWY+LvX6F","media":{"cdm":{"origin_data":"D37EC83B68B1EF5650F106F4A24081A49B4DF9F5678E7EC6A85E161A98A08E4B","origin_data_encrypted_hash":"djEwdQ7rDoX63F/XSRYoVfMgil31wvOsgqJ/0y3L6ge0qUjgoEWFAV92tS7g2/XOOl4MlZzDbTLcfz+Ux3vQ"},"storage_id_salt":"B2E9290C6F6859BEB974309F06D9E137958B4AE07302B17AAE243C417F7EA569","storage_id_salt_encrypted_hash":"djEwWEHlCtv0RH9ELuLgmZKcSQHDo4seZyhpKGt0/mF4uhgBm8r4+FLS4cvv5d0X7vUWQ3ywFYnuRcELhJMT"},"pinned_tabs":"6EE16DC2F5850603ED8BDEA03AEFF18B572EE9FFFBC60EA1A9EED8FA68C0F5DB","pinned_tabs_encrypted_hash":"djEwaDpUbF6uLpBoOnz68xWzcRz2R5GJpdWpdukE7MrYcEc5qb2GFuPyoEP9f8bi3p2mahpwgLPQSTlFWmBz","prefs":{"preference_reset_time":"15C19F277D4917DCF71B31F461B2D0C8DBB3B848428CA9BC3C5C6BC48574AC87","preference_reset_time_encrypted_hash":"djEwpmSiY/R1FpD2G0hbLmraseN8FK4wvjsJU3xe9zs4f+8kBdQg3mTKnqBSGabW2k760oOpgX1pQ1yAU2zK"},"safebrowsing":{"incidents_sent":"939F8A46FD612B2F11302FE860AA28AC2BB97403B61A579D55FF125171603A19","incidents_sent_encrypted_hash":"djEwPqQs0EeeemuMxF4QRdgluDnXiPcp0FxKeHnKOR1AeMikaSGoDbYqZKCvxTwUfZLAJXqkERCUkgKpPqQd"},"schedule_to_flush_to_disk":"73B00FF74CAD3D8DEB71835592DE11F42B31D48B72EAE99CDAA89E3332F146A6","schedule_to_flush_to_disk_encrypted_hash":"djEw1ux3baFg33w4q7EWXfIciOVU+/CvwcpoFWjmdRuSS2fz/EScopHpR5uq4kgjdEX1uSgKGEKH6fpL3NV/","search_provider_overrides":"F89D61352813CEF1214763E778885A6264DB78F18556CF125DB7E7269A975DEA","search_provider_overrides_encrypted_hash":"djEwhBzRmXmBGPO436PJUkEkNvO7V9LxdpAplpzFSl3ziqR9GnqYZpusQ+kTCzuRlSIAemA7KfXHqqDurvnL","session":{"restore_on_startup":"01D0DC40F4692ABDB2D5ACF617641F9F792DFAF368CDC870D173736D7C3A2DF1","restore_on_startup_encrypted_hash":"djEwlpI6T8rgpeHmCYunwO957It/faD+czJuOkR8Xp0kIgSDm0YusWO+shh4tXZfAdOBNqpC9QpBi3DrMKzw","startup_urls":"D1197E0D92DA48044470C7DAA2988BB7F626A57B23FF417848ECDAC833189798","startup_urls_encrypted_hash":"djEwFgjmg0tb9dlPDyUaI2u1RpPI+RrAEs29mrGBVgy7YT5zy7JNGcQndcr+RRgEFYIYpuCAqDu+ZOCwtKwf"}},"super_mac":"D86A358CBF6AA8A4008064A52FE46A81B5E3DC5916B65D93F711AC090A4A22E6"},"schedule_to_flush_to_disk":"13422904239960103"} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Session Storage/000003.log b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Session Storage/000003.log index c7453a1..de23708 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Session Storage/000003.log and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Session Storage/000003.log differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Session Storage/LOG b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Session Storage/LOG index e69de29..a70c1ba 100644 --- a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Session Storage/LOG +++ b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Session Storage/LOG @@ -0,0 +1,3 @@ +2026/05/10-18:30:40.323 292c Reusing MANIFEST C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Session Storage/MANIFEST-000001 +2026/05/10-18:30:40.324 292c Recovering log #3 +2026/05/10-18:30:40.325 292c Reusing old log C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Session Storage/000003.log diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Session Storage/LOG.old b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Session Storage/LOG.old index 6f6ff2a..149d0de 100644 --- a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Session Storage/LOG.old +++ b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Session Storage/LOG.old @@ -1,2 +1,3 @@ -2026/05/09-20:34:51.238 26dc Creating DB C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Session Storage since it was missing. -2026/05/09-20:34:51.244 26dc Reusing MANIFEST C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Session Storage/MANIFEST-000001 +2026/05/10-01:32:21.088 744 Reusing MANIFEST C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Session Storage/MANIFEST-000001 +2026/05/10-01:32:21.089 744 Recovering log #3 +2026/05/10-01:32:21.090 744 Reusing old log C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Session Storage/000003.log diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Site Characteristics Database/000003.log b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Site Characteristics Database/000003.log index 46580fb..b02e28d 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Site Characteristics Database/000003.log and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Site Characteristics Database/000003.log differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Site Characteristics Database/LOG.old b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Site Characteristics Database/LOG.old index 5b40512..3bd0c2e 100644 --- a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Site Characteristics Database/LOG.old +++ b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Site Characteristics Database/LOG.old @@ -1,2 +1,3 @@ -2026/05/09-20:34:50.273 249c Creating DB C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Site Characteristics Database since it was missing. -2026/05/09-20:34:50.276 249c Reusing MANIFEST C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Site Characteristics Database/MANIFEST-000001 +2026/05/10-01:32:20.822 1518 Reusing MANIFEST C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Site Characteristics Database/MANIFEST-000001 +2026/05/10-01:32:20.823 1518 Recovering log #3 +2026/05/10-01:32:20.823 1518 Reusing old log C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Site Characteristics Database/000003.log diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Sync Data/LevelDB/LOG.old b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Sync Data/LevelDB/LOG.old index 6410891..8aa050a 100644 --- a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Sync Data/LevelDB/LOG.old +++ b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/Sync Data/LevelDB/LOG.old @@ -1,2 +1,3 @@ -2026/05/09-20:34:50.269 344c Creating DB C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Sync Data\LevelDB since it was missing. -2026/05/09-20:34:50.272 344c Reusing MANIFEST C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Sync Data\LevelDB/MANIFEST-000001 +2026/05/10-01:32:20.822 1df8 Reusing MANIFEST C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Sync Data\LevelDB/MANIFEST-000001 +2026/05/10-01:32:20.823 1df8 Recovering log #3 +2026/05/10-01:32:20.823 1df8 Reusing old log C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\Sync Data\LevelDB/000003.log diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/favorites_diagnostic.log b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/favorites_diagnostic.log index 6d4bd06..892cbf7 100644 --- a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/favorites_diagnostic.log +++ b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/favorites_diagnostic.log @@ -1,2 +1,3 @@ 2026-05-09 18:34:50.289: [INFO] OnDoneLoading sync enabled: 0 2026-05-09 23:32:20.836: [INFO] OnDoneLoading sync enabled: 0 +2026-05-10 16:30:39.960: [INFO] OnDoneLoading sync enabled: 0 diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/shared_proto_db/000003.log b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/shared_proto_db/000003.log index cc51647..f2ce9e0 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/shared_proto_db/000003.log and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/shared_proto_db/000003.log differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/shared_proto_db/LOG.old b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/shared_proto_db/LOG.old index 96fcbff..4657c83 100644 --- a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/shared_proto_db/LOG.old +++ b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/shared_proto_db/LOG.old @@ -1,2 +1,3 @@ -2026/05/09-20:34:50.297 228c Creating DB C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\shared_proto_db since it was missing. -2026/05/09-20:34:50.299 228c Reusing MANIFEST C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\shared_proto_db/MANIFEST-000001 +2026/05/10-01:32:20.839 8ac Reusing MANIFEST C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\shared_proto_db/MANIFEST-000001 +2026/05/10-01:32:20.839 8ac Recovering log #3 +2026/05/10-01:32:20.840 8ac Reusing old log C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\shared_proto_db/000003.log diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/shared_proto_db/metadata/000003.log b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/shared_proto_db/metadata/000003.log index 335f3dc..a3e99b8 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/shared_proto_db/metadata/000003.log and b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/shared_proto_db/metadata/000003.log differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/shared_proto_db/metadata/LOG.old b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/shared_proto_db/metadata/LOG.old index 53563b4..6f974e6 100644 --- a/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/shared_proto_db/metadata/LOG.old +++ b/.metadata/.plugins/org.eclipse.swt/EBWebView/Default/shared_proto_db/metadata/LOG.old @@ -1,2 +1,3 @@ -2026/05/09-20:34:50.293 228c Creating DB C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\shared_proto_db\metadata since it was missing. -2026/05/09-20:34:50.296 228c Reusing MANIFEST C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\shared_proto_db\metadata/MANIFEST-000001 +2026/05/10-01:32:20.838 8ac Reusing MANIFEST C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\shared_proto_db\metadata/MANIFEST-000001 +2026/05/10-01:32:20.838 8ac Recovering log #3 +2026/05/10-01:32:20.838 8ac Reusing old log C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.swt\EBWebView\Default\shared_proto_db\metadata/000003.log diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/GrShaderCache/data_0 b/.metadata/.plugins/org.eclipse.swt/EBWebView/GrShaderCache/data_0 index 19d9ada..fe0d1e6 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/GrShaderCache/data_0 and b/.metadata/.plugins/org.eclipse.swt/EBWebView/GrShaderCache/data_0 differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/GrShaderCache/data_1 b/.metadata/.plugins/org.eclipse.swt/EBWebView/GrShaderCache/data_1 index e8c9cba..6ee3712 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/GrShaderCache/data_1 and b/.metadata/.plugins/org.eclipse.swt/EBWebView/GrShaderCache/data_1 differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/GrShaderCache/data_3 b/.metadata/.plugins/org.eclipse.swt/EBWebView/GrShaderCache/data_3 index 62ebf3b..aa796d3 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/GrShaderCache/data_3 and b/.metadata/.plugins/org.eclipse.swt/EBWebView/GrShaderCache/data_3 differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/GrShaderCache/f_000001 b/.metadata/.plugins/org.eclipse.swt/EBWebView/GrShaderCache/f_000001 new file mode 100644 index 0000000..864f6fc Binary files /dev/null and b/.metadata/.plugins/org.eclipse.swt/EBWebView/GrShaderCache/f_000001 differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/GraphiteDawnCache/data_1 b/.metadata/.plugins/org.eclipse.swt/EBWebView/GraphiteDawnCache/data_1 index cca57b3..20fd5d4 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/GraphiteDawnCache/data_1 and b/.metadata/.plugins/org.eclipse.swt/EBWebView/GraphiteDawnCache/data_1 differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Local State b/.metadata/.plugins/org.eclipse.swt/EBWebView/Local State index 813035f..9f55b3b 100644 --- a/.metadata/.plugins/org.eclipse.swt/EBWebView/Local State +++ b/.metadata/.plugins/org.eclipse.swt/EBWebView/Local State @@ -1 +1 @@ -{"accessibility":{"captions":{"common_models_path":"","soda_translation_binary_path":""}},"autofill":{"ablation_seed":"FivBD7Ft8QE="},"breadcrumbs":{"enabled":true,"enabled_time":"13422825290225753"},"default_browser":{"browser_name_enum":6},"desktop_session_duration_tracker":{"last_session_end_timestamp":"1778351759"},"domain_actions_config":"H4sIAAAAAAAAAL1abW8bNxL+K4ZwH5LCWitJm975EASFkUMKNGiQS9ED6p4wS452JyI5NMnV7rrJfz9QL7Y2Wq2XcnAfLEujeWbI4exwXvTXREg95xU6RxInl39NwFpFAgKx8ZPLP7YElHPLikQ7uZz8alT7prHs8b2C9gOCbCfnE8kayEwuJ2Wlqkywnnw5TwfrypPIIgpPlhFWWctVqPLTRXhFptig/zyfrFGEG3MY0HhUQGht/DKS3JoUlR8gfieJKzJ4D7ijRHUrdJ7YTC6ffTmfCJYoEg/oLUl8t3iLK3EV0V/Z5gHjDoANmYBG8qBhDvHbPe4Te7apo2XmZBQZTHVIG36twv5KwdqscKA1ONUOeMEB0Jfg0DKZcBpqKhcpQMNxW5nnsQBFqyGvPuDnxYLECYgXL384GZRpAT7TfjTYoHSn7ctgGIsQiiuZaRKOPS9Gw9YGT1ATELS/V3Oi71WjjSdYazbR39kVY0E15lldQvARl7BCwZYUJ20KJA/G4UOAExnQ6BWBWUHSFsCKKmkHogQHIqBLWVYJobBpahRUElN0cIkuybQS0QKlOEpEqFQVHnGZaUyGpKghSNsIKlyhUZD7jEYHWw1uKdi2CYeiMSS5o67cIkV8a8h8ggSE+btJ2DGHND+36KzChkKKjSwnea116NGETQKQosaR1ymmuqlIqTwtvN3UaBJUONS8wiwf7beuMjW0OukZ9JXhhDX51oQSPUGCnwRsgmAXsElZWO0oulcCouWhUuKA/RYspSnY3WgnXdiBloGT4lZ+V1aM4y+Yi8FK6ABRWT9ct2wQPQl4zO3DfEEqoJuzmfNiMfc1BVGOy8KvNhL+tRbQCVpd2x5ZWBd+sD4UNrEqeK+AzEdswm8ffvGx+OoUqWAKroLPHrLwoJR4zT9OglcgBjxoELsAgTkPOeAgvMb18k8FU0PmRPDNzcla86GIM4xlJ+NV4k/Eh5rCYPQaREvygp0cfAL6BGxr5zdX7yNhzRG2HIfPCEmV+IxsOxgOF9SgfPPuTSd1wLBQNGDvMWgfKkk8YPRBIdt2kIZbjs6WfbLfQEi1fLyQR29GHsmPx0vAIzXieAkL91gJZB4t4UhlnXAY8A2OM8tPscVemzAZyxYNScHGoAixIIfbymGNuaeAftNyOBIo+uTuOo87ou7pJZKGAucaCkOLdu6wwCYxXiiu0XfaI5GQfde/f8yhJV106rAN6RhCOKDCK/LdJO2OegwX9G0nVuvbo5zgCuxmcWvKMf4897H06PRBd7RjGEsmOOzugTJLJu78qLdEhwiQK+wWDJ5u0WUctiqPwoFcbvKvfFyAKAdSE40GnOwcqJAm25DF4GW184QVqCp+vr5+HVf66omWnxv19GzzqWnUfgv83iF2MD9VT66v5XdPz/xUPXs5m+2zd7xhh5i/mM2aF7PZ9XV2Nn/2fDZr/jGbZfuwjTPc8Wu5Zm1Ul2nnBzu+//7t7HVN8lUUuc+45wA71n9qaN4iFWV4tVl8JPxOMpTbz2f7Ava8YSfg4vmLl83F2cWP8d8+774P7JifaJRU6c8qrvhz4zUo9fTirKwK7GDvHOBOy2vS8/rVj89nZ9u3z77/vrO3+/O/U/Zuo+zfUcvnX6LKpxdn/1m/uTiMKOsae46NVexi1fDItv1zLU1Ks5V0MfVT7c00hnBYgga6RZkioifqji/lptjYY0Hg8cVfXdeZAPIep2jBFQaP3th9ha1Eiym9H+EQAp5WCUtcoJHoHNxyUv0t0VNh0J2olk0BTpq0ml9WYhn/Ck5BPVxt9WAc4jS+CFRq6llRAErr2W6ry9FOtuH3rZHbhy9FGRU8rdHlCC7QEtV4tRHJ+ScM3lZ5Esw6XlCaJutYcwLC+ABxGphiCkVmiZLMeDWpozkN9qbC4zlNTy93W5CNBpw6/QNrfZY8arRoHJCK/UpIGkdxFXIHD6RHfc3nBtVAZdnT6kRwoswMrNIixhbXQslJMePEafKSV5QUZ6J/x+tofSdFbaM9xAd2UGDyYfuVSJjw+rY4ni33dZdFGeealYmDhf9Ta/bB/s63G6O2oHVyH/yB37P0JADrfmqmMO2es+BCvFBTcb5km+XP81OAtWPWyUCFBYi2RFChTAk3OSiFIXhRVgqnjE7RMt6T5ehzJwXGL9sUpRoKElMVi4aH+vM/m/16nrx+s82vP7YWN9/v5fB3qgYQvR3/yqObQxGb/ml5+1XpWONvP3XSDsf1thM+OZ9YCOVcw3p08MfkQoBDdFPpEDS6i5I1Xkz+7DVcn2xPquQKQ8BpfOKOekgfVrdZ3d7i8OThHre1IcoCP/ISzQe0CgRqNPvl26Eta8zn8bc8i63lUkcVjrU9yP0dSklDDfpeVECFhQNbkpE0MIs+RAfS6Hmxwa1f15QUEbbyJZoi3iipy46qDNcG6ySN6yKJK8vGgaFUZF2yQs8a22ogO+zHgl8qIJVk4ojzoDXnNDgy6gWiqGBFPklhdZt5WwVDy7VhXZWi0MiwSl6kIm3BCYKBmXU/1PIKHBpeJi4TfQAJPNjGPUSyaiwsyQcwGfssiNGPSfwtSeoxsEpc3trDGvI5mKEB3wFOV6ECtaiMzEBSaCEnp0CApZB+IiMmjL24ERVXP1Cj92iKoeToZyNYkymuQKnusfT88O7YpG2nfBvv7ebz3q36lZYtH22pcxHJe+wbgT8Z2Y/byJ+DkfOvRHQvki//A5h5bsOPLQAA","edge":{"manageability":{"edge_last_active_time":"13422825356271814"},"retrigger_features":[],"tab_stabs":{"closed_without_unfreeze_never_unfrozen":0,"closed_without_unfreeze_previously_unfrozen":0,"discard_without_unfreeze_never_unfrozen":0,"discard_without_unfreeze_previously_unfrozen":0},"tab_stats":{"frozen_daily":0,"unfrozen_daily":0}},"edge_ci":{"num_healthy_browsers_since_failure":2},"hardware_acceleration_mode_previous":true,"identity_combined_status":{"aad":1,"ad":1},"legacy":{"profile":{"name":{"migrated":true}}},"local":{"password_hash_data_list":[]},"optimization_guide":{"model_execution":{"last_usage_by_feature":{}},"model_store_metadata":{},"on_device":{"last_version":"147.0.3912.98","model_crash_count":0}},"os_crypt":{"audit_enabled":true,"encrypted_key":"RFBBUEkBAAAA0Iyd3wEV0RGMegDAT8KX6wEAAACiPxEnG1/CSY1jnLc4XUkbEAAAAB4AAABNAGkAYwByAG8AcwBvAGYAdAAgAEUAZABnAGUAAAAQZgAAAAEAACAAAAA5m+RU369qlDRNJw536rgYo0g7lbaG+8+1UYQKgVe5UwAAAAAOgAAAAAIAACAAAACdbioUEYXU0KmGVxkd+xO2PXzKDPnf35QIuaeuTDcNSjAAAAANUwQ1tAvl+bBa1dzjsYMVbdkI+ZGjD+J775LQjaGPbvJrVu+gH3jebbxz/FxLsl9AAAAAASWIjwzqxz0hwJWQhbROUd+FJv0v6KT2yDPhU5+s5kl2GMzbnJrtQFGQVlo7h8M6D6TLD0LO4nAWEO1sQzurdQ=="},"performance_intervention":{"last_daily_sample":"13422825290280494"},"phoenix":{"user_laf_toggle_state_static":2},"policy":{"last_statistics_update":"13422825290224528"},"profile":{"info_cache":{"Default":{"active_time":1778351749.85963,"avatar_icon":"chrome://theme/IDR_PROFILE_AVATAR_20","background_apps":false,"edge_account_cid":"","edge_account_environment":0,"edge_account_environment_string":"","edge_account_first_name":"","edge_account_last_name":"","edge_account_oid":"","edge_account_sovereignty":0,"edge_account_tenant_id":"","edge_account_type":0,"edge_create_profile_shortcut":false,"edge_non_signin_profile_type":1,"edge_profile_can_be_deleted":true,"edge_profile_can_be_edited":true,"edge_test_on_premises":false,"edge_wam_aad_for_app_account_type":0,"enterprise_label":"","force_signin_profile_locked":false,"gaia_given_name":"","gaia_id":"","gaia_name":"","hosted_domain":"","is_consented_primary_account":false,"is_ephemeral":false,"is_glic_eligible":false,"is_managed":0,"is_using_default_avatar":true,"is_using_default_name":true,"managed_user_id":"","metrics_bucket_index":1,"name":"Profile 1","signin.with_credential_provider":false,"user_name":""}},"last_active_profiles":["Default"],"metrics":{"next_bucket_index":2},"profile_counts_reported":"13422825290222694","profiles_order":["Default"]},"profile_network_context_service":{"http_cache_finch_experiment_groups":"None None None None"},"profiles":{"edge":{"guided_switch_pref":[],"multiple_profiles_with_same_account":false},"edge_sso_info":{"msa_first_profile_key":"Default","msa_sso_algo_state":1},"signin_last_seen_version":"147.0.3912.98","signin_last_updated_time":1778351690.277739},"sentinel_creation_time":"0","session_id_generator_last_value":"339850194","signin":{"active_accounts_last_emitted":"13422825290183908"},"startup_boost":{"last_browser_open_time":"13422848006138729"},"subresource_filter":{"ruleset_version":{"checksum":860988201,"content":"10.34.0.84","format":37}},"tab_stats":{"discards_expired":0,"discards_external":0,"discards_proactive":0,"discards_urgent":0,"last_daily_sample":"13422825290211560","max_tabs_per_window":1,"reloads_expired":0,"reloads_external":0,"reloads_urgent":0,"total_tab_count_max":4,"window_count_max":4},"telemetry_client":{"cloned_install":{"user_data_dir_id":11297294},"governance":{"last_dma_change_date":"13422825290206518","last_known_cps":0},"host_telclient_path":"QzpcUHJvZ3JhbSBGaWxlcyAoeDg2KVxNaWNyb3NvZnRcRWRnZVdlYlZpZXdcQXBwbGljYXRpb25cMTQ3LjAuMzkxMi45OFx0ZWxjbGllbnQuZGxs","sample_id":28518521},"uninstall_metrics":{"installation_date2":"1778351690"},"updateclientdata":{"apps":{"alpjnmnfbgfkmmpcfpejmmoebdndedno":{"cohort":"","cohortname":"","fp":"","installdate":-1,"max_pv":"0.0.0.0","pv":"46.0.0.0"},"eeobbhfgfagbclfofmgbdfoicabjdbkn":{"cohort":"","cohortname":"","fp":"","installdate":-1,"max_pv":"0.0.0.0","pv":"1.0.0.10"},"fgbafbciocncjfbbonhocjaohoknlaco":{"cohort":"","cohortname":"","fp":"","installdate":-1,"max_pv":"0.0.0.0","pv":"2026.3.23.1"},"jbfaflocpnkhbgcijpkiafdpbjkedane":{"cohort":"","cohortname":"","installdate":-1},"kpfehajjjbbcifeehjgfgnabifknmdad":{"cohort":"","cohortname":"","fp":"","installdate":-1,"max_pv":"0.0.0.0","pv":"120.0.6050.0"},"laoigpblnllgcgjnjnllmfolckpjlhki":{"cohort":"","cohortname":"","installdate":-1},"ndikpojcjlepofdkaaldkinkjbeeebkl":{"cohort":"","cohortname":"","fp":"","installdate":-1,"max_pv":"0.0.0.0","pv":"10.34.0.84"},"oankkpibpaokgecfckkdkgaoafllipag":{"cohort":"","cohortname":"","fp":"","installdate":-1,"max_pv":"0.0.0.0","pv":"6498.2025.9.4"},"ohckeflnhegojcjlcpbfpciadgikcohk":{"cohort":"","cohortname":"","fp":"","installdate":-1,"max_pv":"0.0.0.0","pv":"0.0.1.7"},"ojblfafjmiikbkepnnolpgbbhejhlcim":{"cohort":"","cohortname":"","installdate":-1}}},"updateclientlastupdatecheckerror":0,"updateclientlastupdatecheckerrorcategory":0,"updateclientlastupdatecheckerrorextracode1":0,"user_experience_metrics":{"chrome_download_action_count":0,"client_id2":"{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}C:\\Users\\eslusarz0s:05D2D359-2B14-4013-B5BA-6ED5F41F2761","diagnostics":{"last_data_collection_level_on_launch":1},"limited_entropy_randomization_source":"94FB2CB93E02CD91F8FEEE6A7854498F","low_entropy_source3":4436,"machine_id":492731,"payload_counter":1,"pseudo_low_entropy_source":452,"reporting_enabled":false,"reset_client_id_deterministic":true,"session_id":1,"stability":{"browser_last_live_timestamp":"13422847640804626","stats_buildtime":"1777534620","stats_version":"147.0.3912.98-64","system_crash_count":0}},"variations_compressed_seed":"H4sIAAAAAAAAAJVVbXPiNhD+Kxl9tlLL79DpB8JLjh7c0QCXdnqZjLDXjiayRCUZwmT47x1ZhIT0mFz5wHh3n93n2dVafkbD/hx1n1GfMxCmL0XJqkZRw6RYrgtqYLgBYSxiANzQiayGgq44FKhbUq7BQ6OG8zmY85GRgn8aEPkOdYnv7z00fMp5U8DwyYASlDvScaHHYiIr1DWqAQ8570RWC6oqMKiLoKjgXhvLgWyVogIHatUxbQMTmVP+BcxWqsdenoPW/QfIHy2icIgRUNMo0Kj7NzqD1uhu76HtJphTUazk05nssZacGjiAoBiXitbgkg9yRozDfKcN1G/Kc6bNmZLv8QOmIDdS7cYG3KFccekquL4slxv77begVxTMYiifKWnTPzPOoVgAhxqM2llSEO85a30Lq28Mth/nW7Z5kT84xnPl7MFYVAv/YznuW+Ca2uEYUBb0jAStAXVR/kCFAI48tKG8sZ4R2nvHMKxl/vAmGPru9xajwCgqdM1Muxn3UtxvmYJ7w2qQjbmvGedMQy5Fod+UCmwVK/DrBpRiBSw/T/W5jpaP9bXc2GUVOUzs8d15ZzpqwS81W+gr55TlSmpZmssRU9rMqDK7w/bd2FdEmxvQDTce8Uj866m89jU8K9BFe5zL7UfyWuiHAm9hdaXkVoO6nClp5KopL5efp5duOWdKloyD51udP5FkqNGXE1nNDVXG873o/yT1Zb3mYKDNc0M5buRMrufUhtt/JqpzAxpJlcMx7Yj2UK3tFQPaLAUrpaoHTBvFVo19Dz4xbWSlaH1+npWSzTo6yngzS3K6pi0w/glgASVtuLm2+LPwu/3hAmwX6aYRrxfhsF6b3Y9D/x0MQKly1FabQ1WDMO0lY8EjJYVhoHpNwUDklvqLFFbJtDfucY66qL3mkYdub4enDp2vy8Z6/Pa5aqgqXowNqPZxGiZxXyo4zexLYUCYOagNy0G/44HVn4tT1xUT1anHtn7qOe7aUlvug3/voU9Ai/Y8n9FwQSvURd8R/PJ1MMtnoXnQo3U4Hq9JXk5mtfx9ES2X5SYjV+VqOdwkf12Pf/uOLN/TmrXTRHNqvAu/czGlu4vAD5IL0umGUTeOL66ni7a3Rhi168vCDnNsXXbJG33wtFfSy4dvPHC67NcZzfANJmlM4sTHBBOvtf0siNPWto4BjoIwy3CAE3T6bTxkh0kcERzj2GWHJI4yggnOnB1HhKQEJ7hzsIMs8H0c4sjZhIRxSHB0iCdZlEa4g0ngTBL4CQ4iHITO9hOS4gwHTmsUh2mIiY+zFP14b51IP42zJMYRztD7hTx0QZKUtJWI73RFaeiT5LUPEnc6WYzJS5+EhB0S4yB2Y7IknSwMI0xw4qQmfpphgsOjlR6zEz/LAkywazIK/SzA4SEWBWlkqwRov/8X5nU8zUYJAAA=","variations_config_ids":"P-R-1736541-5-5,P-R-1315481-1-8,P-R-1541171-6-9,P-R-1528200-3-4,P-R-1113531-4-9,P-R-68474-9-12,P-R-61206-24-23,P-R-60617-8-21,P-R-45373-10-87,P-R-1075865-4-8","variations_crash_streak":1,"variations_failed_to_fetch_seed_streak":0,"variations_google_groups":{"Default":[]},"variations_last_fetch_time":"13422825293001203","variations_last_runtime_fetch_time":"13422825293006739","variations_runtime_compressed_seed":"H4sIAAAAAAAAAG2Py07DMBBF/2W2ZFQ/Jo4diQ1pIGFVqq6gLEJjTBF1mpdEqPLvqLW66/Lcc6U7c4Ks8Z97Vy57SGGFa+TMSEExchQUhUArRshRJhdOtGQKOXIdUCpxwWCV5CpGQqEuGEtBEgUGSYwZifIqpU6IUKMJVnJtDAqDZEy0wiVyHmvFCQVeL5E6UQwFcgYR5LWz69EP+4MNT0B6gvxwHKabxvrq48c+2moYO9tD+gbWdjt4n+cIClvVtuvPtawZ/dBNWVNbSKHcnIc2lYMUtvASP7PClbtGqwffHkXjstxNrL/7Jfpqiz/vn8R3vXhdtO5+CzDP/+zFLFRfAQAA","variations_runtime_config_ids":"P-R-1093245-1-24,P-R-108604-1-37,P-R-78306-1-18,P-R-73626-1-17,P-R-63165-4-26,P-R-53243-2-7,P-R-40093-3-26,P-R-38744-8-97,P-R-31899-29-499,P-D-1158614-2-4,P-R-1038760-2-10","variations_seed_client_version_at_store":"147.0.3912.98","variations_seed_date":"13422825296000000","variations_seed_etag":"\"e/ODPcP3thsFp3IIp1cfLPmoJT4UUfv81BfbUEv6YGI=\"","variations_seed_milestone":147,"variations_seed_runtime_etag":"\"Q5J0HgIco86Bnqp2ogCEgy0s+x44hqHznnG2jd/Z/qg=\"","variations_seed_runtime_serial_number":"\"Q5J0HgIco86Bnqp2ogCEgy0s+x44hqHznnG2jd/Z/qg=\"","variations_seed_serial_number":"\"e/ODPcP3thsFp3IIp1cfLPmoJT4UUfv81BfbUEv6YGI=\"","variations_seed_signature":"","was":{"restarted":false}} \ No newline at end of file +{"accessibility":{"captions":{"common_models_path":"","soda_translation_binary_path":""}},"autofill":{"ablation_seed":"FivBD7Ft8QE="},"breadcrumbs":{"enabled":true,"enabled_time":"13422825290225753"},"default_browser":{"browser_name_enum":6},"desktop_session_duration_tracker":{"last_session_end_timestamp":"1778491645"},"domain_actions_config":"H4sIAAAAAAAAAL1abW8bNxL+K4ZwH5LCWitJm975EASFkUMKNGiQS9ED6p4wS452JyI5NMnV7rrJfz9QL7Y2Wq2XcnAfLEujeWbI4exwXvTXREg95xU6RxInl39NwFpFAgKx8ZPLP7YElHPLikQ7uZz8alT7prHs8b2C9gOCbCfnE8kayEwuJ2Wlqkywnnw5TwfrypPIIgpPlhFWWctVqPLTRXhFptig/zyfrFGEG3MY0HhUQGht/DKS3JoUlR8gfieJKzJ4D7ijRHUrdJ7YTC6ffTmfCJYoEg/oLUl8t3iLK3EV0V/Z5gHjDoANmYBG8qBhDvHbPe4Te7apo2XmZBQZTHVIG36twv5KwdqscKA1ONUOeMEB0Jfg0DKZcBpqKhcpQMNxW5nnsQBFqyGvPuDnxYLECYgXL384GZRpAT7TfjTYoHSn7ctgGIsQiiuZaRKOPS9Gw9YGT1ATELS/V3Oi71WjjSdYazbR39kVY0E15lldQvARl7BCwZYUJ20KJA/G4UOAExnQ6BWBWUHSFsCKKmkHogQHIqBLWVYJobBpahRUElN0cIkuybQS0QKlOEpEqFQVHnGZaUyGpKghSNsIKlyhUZD7jEYHWw1uKdi2CYeiMSS5o67cIkV8a8h8ggSE+btJ2DGHND+36KzChkKKjSwnea116NGETQKQosaR1ymmuqlIqTwtvN3UaBJUONS8wiwf7beuMjW0OukZ9JXhhDX51oQSPUGCnwRsgmAXsElZWO0oulcCouWhUuKA/RYspSnY3WgnXdiBloGT4lZ+V1aM4y+Yi8FK6ABRWT9ct2wQPQl4zO3DfEEqoJuzmfNiMfc1BVGOy8KvNhL+tRbQCVpd2x5ZWBd+sD4UNrEqeK+AzEdswm8ffvGx+OoUqWAKroLPHrLwoJR4zT9OglcgBjxoELsAgTkPOeAgvMb18k8FU0PmRPDNzcla86GIM4xlJ+NV4k/Eh5rCYPQaREvygp0cfAL6BGxr5zdX7yNhzRG2HIfPCEmV+IxsOxgOF9SgfPPuTSd1wLBQNGDvMWgfKkk8YPRBIdt2kIZbjs6WfbLfQEi1fLyQR29GHsmPx0vAIzXieAkL91gJZB4t4UhlnXAY8A2OM8tPscVemzAZyxYNScHGoAixIIfbymGNuaeAftNyOBIo+uTuOo87ou7pJZKGAucaCkOLdu6wwCYxXiiu0XfaI5GQfde/f8yhJV106rAN6RhCOKDCK/LdJO2OegwX9G0nVuvbo5zgCuxmcWvKMf4897H06PRBd7RjGEsmOOzugTJLJu78qLdEhwiQK+wWDJ5u0WUctiqPwoFcbvKvfFyAKAdSE40GnOwcqJAm25DF4GW184QVqCp+vr5+HVf66omWnxv19GzzqWnUfgv83iF2MD9VT66v5XdPz/xUPXs5m+2zd7xhh5i/mM2aF7PZ9XV2Nn/2fDZr/jGbZfuwjTPc8Wu5Zm1Ul2nnBzu+//7t7HVN8lUUuc+45wA71n9qaN4iFWV4tVl8JPxOMpTbz2f7Ava8YSfg4vmLl83F2cWP8d8+774P7JifaJRU6c8qrvhz4zUo9fTirKwK7GDvHOBOy2vS8/rVj89nZ9u3z77/vrO3+/O/U/Zuo+zfUcvnX6LKpxdn/1m/uTiMKOsae46NVexi1fDItv1zLU1Ks5V0MfVT7c00hnBYgga6RZkioifqji/lptjYY0Hg8cVfXdeZAPIep2jBFQaP3th9ha1Eiym9H+EQAp5WCUtcoJHoHNxyUv0t0VNh0J2olk0BTpq0ml9WYhn/Ck5BPVxt9WAc4jS+CFRq6llRAErr2W6ry9FOtuH3rZHbhy9FGRU8rdHlCC7QEtV4tRHJ+ScM3lZ5Esw6XlCaJutYcwLC+ABxGphiCkVmiZLMeDWpozkN9qbC4zlNTy93W5CNBpw6/QNrfZY8arRoHJCK/UpIGkdxFXIHD6RHfc3nBtVAZdnT6kRwoswMrNIixhbXQslJMePEafKSV5QUZ6J/x+tofSdFbaM9xAd2UGDyYfuVSJjw+rY4ni33dZdFGeealYmDhf9Ta/bB/s63G6O2oHVyH/yB37P0JADrfmqmMO2es+BCvFBTcb5km+XP81OAtWPWyUCFBYi2RFChTAk3OSiFIXhRVgqnjE7RMt6T5ehzJwXGL9sUpRoKElMVi4aH+vM/m/16nrx+s82vP7YWN9/v5fB3qgYQvR3/yqObQxGb/ml5+1XpWONvP3XSDsf1thM+OZ9YCOVcw3p08MfkQoBDdFPpEDS6i5I1Xkz+7DVcn2xPquQKQ8BpfOKOekgfVrdZ3d7i8OThHre1IcoCP/ISzQe0CgRqNPvl26Eta8zn8bc8i63lUkcVjrU9yP0dSklDDfpeVECFhQNbkpE0MIs+RAfS6Hmxwa1f15QUEbbyJZoi3iipy46qDNcG6ySN6yKJK8vGgaFUZF2yQs8a22ogO+zHgl8qIJVk4ojzoDXnNDgy6gWiqGBFPklhdZt5WwVDy7VhXZWi0MiwSl6kIm3BCYKBmXU/1PIKHBpeJi4TfQAJPNjGPUSyaiwsyQcwGfssiNGPSfwtSeoxsEpc3trDGvI5mKEB3wFOV6ECtaiMzEBSaCEnp0CApZB+IiMmjL24ERVXP1Cj92iKoeToZyNYkymuQKnusfT88O7YpG2nfBvv7ebz3q36lZYtH22pcxHJe+wbgT8Z2Y/byJ+DkfOvRHQvki//A5h5bsOPLQAA","edge":{"manageability":{"edge_last_active_time":"13422965242399717"},"mitigation_manager":{"renderer_app_container_compatible_count":1},"retrigger_features":[],"tab_stabs":{"closed_without_unfreeze_never_unfrozen":0,"closed_without_unfreeze_previously_unfrozen":0,"discard_without_unfreeze_never_unfrozen":0,"discard_without_unfreeze_previously_unfrozen":0},"tab_stats":{"frozen_daily":0,"unfrozen_daily":0}},"edge_ci":{"num_healthy_browsers_since_failure":3},"hardware_acceleration_mode_previous":true,"identity_combined_status":{"aad":1,"ad":1},"legacy":{"profile":{"name":{"migrated":true}}},"local":{"password_hash_data_list":[]},"network_time":{"network_time_mapping":{"local":1.778505320058342e+12,"network":1.778505325e+12,"ticks":77277954672.0,"uncertainty":1712561.0}},"optimization_guide":{"model_execution":{"last_usage_by_feature":{}},"model_store_metadata":{},"on_device":{"last_version":"147.0.3912.98","model_crash_count":0}},"os_crypt":{"audit_enabled":true,"encrypted_key":"RFBBUEkBAAAA0Iyd3wEV0RGMegDAT8KX6wEAAACiPxEnG1/CSY1jnLc4XUkbEAAAAB4AAABNAGkAYwByAG8AcwBvAGYAdAAgAEUAZABnAGUAAAAQZgAAAAEAACAAAAA5m+RU369qlDRNJw536rgYo0g7lbaG+8+1UYQKgVe5UwAAAAAOgAAAAAIAACAAAACdbioUEYXU0KmGVxkd+xO2PXzKDPnf35QIuaeuTDcNSjAAAAANUwQ1tAvl+bBa1dzjsYMVbdkI+ZGjD+J775LQjaGPbvJrVu+gH3jebbxz/FxLsl9AAAAAASWIjwzqxz0hwJWQhbROUd+FJv0v6KT2yDPhU5+s5kl2GMzbnJrtQFGQVlo7h8M6D6TLD0LO4nAWEO1sQzurdQ=="},"performance_intervention":{"last_daily_sample":"13422913239998106"},"phoenix":{"user_laf_toggle_state_static":2},"policy":{"last_statistics_update":"13422911690226895"},"profile":{"info_cache":{"Default":{"active_time":1778491640.805231,"avatar_icon":"chrome://theme/IDR_PROFILE_AVATAR_20","background_apps":false,"edge_account_cid":"","edge_account_environment":0,"edge_account_environment_string":"","edge_account_first_name":"","edge_account_last_name":"","edge_account_oid":"","edge_account_sovereignty":0,"edge_account_tenant_id":"","edge_account_type":0,"edge_create_profile_shortcut":false,"edge_non_signin_profile_type":1,"edge_profile_can_be_deleted":true,"edge_profile_can_be_edited":true,"edge_test_on_premises":false,"edge_wam_aad_for_app_account_type":0,"enterprise_label":"","force_signin_profile_locked":false,"gaia_given_name":"","gaia_id":"","gaia_name":"","hosted_domain":"","is_consented_primary_account":false,"is_ephemeral":false,"is_glic_eligible":false,"is_managed":0,"is_using_default_avatar":true,"is_using_default_name":true,"managed_user_id":"","metrics_bucket_index":1,"name":"Profile 1","signin.with_credential_provider":false,"user_name":""}},"last_active_profiles":["Default"],"metrics":{"next_bucket_index":2},"profile_counts_reported":"13422911690225981","profiles_order":["Default"]},"profile_network_context_service":{"http_cache_finch_experiment_groups":"None None None None"},"profiles":{"edge":{"guided_switch_pref":[],"multiple_profiles_with_same_account":false},"edge_sso_info":{"msa_first_profile_key":"Default","msa_sso_algo_state":1},"signin_last_seen_version":"147.0.3912.98","signin_last_updated_time":1778438139.955908},"sentinel_creation_time":"0","session_id_generator_last_value":"339850313","signin":{"active_accounts_last_emitted":"13422911690194951"},"startup_boost":{"last_browser_open_time":"13422987292739334"},"subresource_filter":{"ruleset_version":{"checksum":860988201,"content":"10.34.0.84","format":37}},"tab_stats":{"discards_expired":0,"discards_external":0,"discards_proactive":0,"discards_urgent":0,"last_daily_sample":"13422913239945839","max_tabs_per_window":1,"reloads_expired":0,"reloads_external":0,"reloads_urgent":0,"total_tab_count_max":10,"window_count_max":10},"telemetry_client":{"cloned_install":{"user_data_dir_id":11297294},"governance":{"last_dma_change_date":"13422825290206518","last_known_cps":0},"host_telclient_path":"QzpcUHJvZ3JhbSBGaWxlcyAoeDg2KVxNaWNyb3NvZnRcRWRnZVdlYlZpZXdcQXBwbGljYXRpb25cMTQ3LjAuMzkxMi45OFx0ZWxjbGllbnQuZGxs","sample_id":28518521},"uninstall_metrics":{"installation_date2":"1778351690"},"updateclientdata":{"apps":{"alpjnmnfbgfkmmpcfpejmmoebdndedno":{"cohort":"rrf@0.74","cohortname":"","fp":"","installdate":-1,"max_pv":"0.0.0.0","pv":"46.0.0.0"},"eeobbhfgfagbclfofmgbdfoicabjdbkn":{"cohort":"rrf@0.29","cohortname":"","fp":"","installdate":-1,"max_pv":"0.0.0.0","pv":"1.0.0.10"},"fgbafbciocncjfbbonhocjaohoknlaco":{"cohort":"rrf@0.48","cohortname":"","fp":"","installdate":-1,"max_pv":"0.0.0.0","pv":"2026.3.23.1"},"jbfaflocpnkhbgcijpkiafdpbjkedane":{"cohort":"rrf@0.05","cohortname":"","installdate":-1},"kpfehajjjbbcifeehjgfgnabifknmdad":{"cohort":"rrf@0.76","cohortname":"","fp":"","installdate":-1,"max_pv":"0.0.0.0","pv":"120.0.6050.0"},"laoigpblnllgcgjnjnllmfolckpjlhki":{"cohort":"rrf@0.96","cohortname":"","installdate":-1},"ndikpojcjlepofdkaaldkinkjbeeebkl":{"cohort":"rrf@0.35","cohortname":"","fp":"","installdate":-1,"max_pv":"0.0.0.0","pv":"10.34.0.84"},"oankkpibpaokgecfckkdkgaoafllipag":{"cohort":"rrf@0.68","cohortname":"","fp":"","installdate":-1,"max_pv":"0.0.0.0","pv":"6498.2025.9.4"},"ohckeflnhegojcjlcpbfpciadgikcohk":{"cohort":"rrf@0.36","cohortname":"","fp":"","installdate":-1,"max_pv":"0.0.0.0","pv":"0.0.1.7"},"ojblfafjmiikbkepnnolpgbbhejhlcim":{"cohort":"rrf@0.85","cohortname":"","installdate":-1}}},"updateclientlastupdatecheckerror":-105,"updateclientlastupdatecheckerrorcategory":5,"updateclientlastupdatecheckerrorextracode1":0,"user_experience_metrics":{"chrome_download_action_count":0,"client_id2":"{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}C:\\Users\\eslusarz0s:05D2D359-2B14-4013-B5BA-6ED5F41F2761","diagnostics":{"last_data_collection_level_on_launch":1},"limited_entropy_randomization_source":"94FB2CB93E02CD91F8FEEE6A7854498F","low_entropy_source3":4436,"machine_id":492731,"payload_counter":1,"pseudo_low_entropy_source":452,"reporting_enabled":false,"reset_client_id_deterministic":true,"session_id":2,"stability":{"browser_last_live_timestamp":"13422986728606217","exited_cleanly":true,"stats_buildtime":"1777534620","stats_version":"147.0.3912.98-64","system_crash_count":0}},"variations_compressed_seed":"safe_seed_content","variations_config_ids":"P-R-1736541-5-5,P-R-1315481-1-8,P-R-1541171-6-9,P-R-1528200-3-4,P-R-1113531-4-9,P-R-68474-9-12,P-R-61206-24-23,P-R-60617-8-21,P-R-45373-10-87,P-R-1075865-4-8","variations_crash_streak":0,"variations_failed_to_fetch_seed_streak":1,"variations_google_groups":{"Default":[]},"variations_last_fetch_time":"13422975320478873","variations_last_runtime_fetch_time":"13422978920358685","variations_runtime_compressed_seed":"H4sIAAAAAAAAAG2Py07DMBBF/2W2ZFQ/Jo4diQ1pIGFVqq6gLEJjTBF1mpdEqPLvqLW66/Lcc6U7c4Ks8Z97Vy57SGGFa+TMSEExchQUhUArRshRJhdOtGQKOXIdUCpxwWCV5CpGQqEuGEtBEgUGSYwZifIqpU6IUKMJVnJtDAqDZEy0wiVyHmvFCQVeL5E6UQwFcgYR5LWz69EP+4MNT0B6gvxwHKabxvrq48c+2moYO9tD+gbWdjt4n+cIClvVtuvPtawZ/dBNWVNbSKHcnIc2lYMUtvASP7PClbtGqwffHkXjstxNrL/7Jfpqiz/vn8R3vXhdtO5+CzDP/+zFLFRfAQAA","variations_runtime_config_ids":"P-R-1093245-1-24,P-R-108604-1-37,P-R-78306-1-18,P-R-73626-1-17,P-R-63165-4-26,P-R-53243-2-7,P-R-40093-3-26,P-R-38744-8-97,P-R-31899-29-499,P-D-1158614-2-4,P-R-1038760-2-10","variations_safe_compressed_seed":"H4sIAAAAAAAAAJVVbXPiNhD+Kxl9tlLL79DpB8JLjh7c0QCXdnqZjLDXjiayRCUZwmT47x1ZhIT0mFz5wHh3n93n2dVafkbD/hx1n1GfMxCmL0XJqkZRw6RYrgtqYLgBYSxiANzQiayGgq44FKhbUq7BQ6OG8zmY85GRgn8aEPkOdYnv7z00fMp5U8DwyYASlDvScaHHYiIr1DWqAQ8570RWC6oqMKiLoKjgXhvLgWyVogIHatUxbQMTmVP+BcxWqsdenoPW/QfIHy2icIgRUNMo0Kj7NzqD1uhu76HtJphTUazk05nssZacGjiAoBiXitbgkg9yRozDfKcN1G/Kc6bNmZLv8QOmIDdS7cYG3KFccekquL4slxv77begVxTMYiifKWnTPzPOoVgAhxqM2llSEO85a30Lq28Mth/nW7Z5kT84xnPl7MFYVAv/YznuW+Ca2uEYUBb0jAStAXVR/kCFAI48tKG8sZ4R2nvHMKxl/vAmGPru9xajwCgqdM1Muxn3UtxvmYJ7w2qQjbmvGedMQy5Fod+UCmwVK/DrBpRiBSw/T/W5jpaP9bXc2GUVOUzs8d15ZzpqwS81W+gr55TlSmpZmssRU9rMqDK7w/bd2FdEmxvQDTce8Uj866m89jU8K9BFe5zL7UfyWuiHAm9hdaXkVoO6nClp5KopL5efp5duOWdKloyD51udP5FkqNGXE1nNDVXG873o/yT1Zb3mYKDNc0M5buRMrufUhtt/JqpzAxpJlcMx7Yj2UK3tFQPaLAUrpaoHTBvFVo19Dz4xbWSlaH1+npWSzTo6yngzS3K6pi0w/glgASVtuLm2+LPwu/3hAmwX6aYRrxfhsF6b3Y9D/x0MQKly1FabQ1WDMO0lY8EjJYVhoHpNwUDklvqLFFbJtDfucY66qL3mkYdub4enDp2vy8Z6/Pa5aqgqXowNqPZxGiZxXyo4zexLYUCYOagNy0G/44HVn4tT1xUT1anHtn7qOe7aUlvug3/voU9Ai/Y8n9FwQSvURd8R/PJ1MMtnoXnQo3U4Hq9JXk5mtfx9ES2X5SYjV+VqOdwkf12Pf/uOLN/TmrXTRHNqvAu/czGlu4vAD5IL0umGUTeOL66ni7a3Rhi168vCDnNsXXbJG33wtFfSy4dvPHC67NcZzfANJmlM4sTHBBOvtf0siNPWto4BjoIwy3CAE3T6bTxkh0kcERzj2GWHJI4yggnOnB1HhKQEJ7hzsIMs8H0c4sjZhIRxSHB0iCdZlEa4g0ngTBL4CQ4iHITO9hOS4gwHTmsUh2mIiY+zFP14b51IP42zJMYRztD7hTx0QZKUtJWI73RFaeiT5LUPEnc6WYzJS5+EhB0S4yB2Y7IknSwMI0xw4qQmfpphgsOjlR6zEz/LAkywazIK/SzA4SEWBWlkqwRov/8X5nU8zUYJAAA=","variations_safe_seed_date":"13422900277000000","variations_safe_seed_fetch_time":"13422975320478873","variations_safe_seed_locale":"en-GB","variations_safe_seed_milestone":147,"variations_safe_seed_signature":"","variations_seed_client_version_at_store":"147.0.3912.98","variations_seed_date":"13422975325000000","variations_seed_etag":"\"e/ODPcP3thsFp3IIp1cfLPmoJT4UUfv81BfbUEv6YGI=\"","variations_seed_milestone":147,"variations_seed_runtime_etag":"\"Q5J0HgIco86Bnqp2ogCEgy0s+x44hqHznnG2jd/Z/qg=\"","variations_seed_runtime_serial_number":"\"Q5J0HgIco86Bnqp2ogCEgy0s+x44hqHznnG2jd/Z/qg=\"","variations_seed_serial_number":"\"e/ODPcP3thsFp3IIp1cfLPmoJT4UUfv81BfbUEv6YGI=\"","variations_seed_signature":"","was":{"restarted":false}} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/ShaderCache/data_1 b/.metadata/.plugins/org.eclipse.swt/EBWebView/ShaderCache/data_1 index 134bf9c..59f2c79 100644 Binary files a/.metadata/.plugins/org.eclipse.swt/EBWebView/ShaderCache/data_1 and b/.metadata/.plugins/org.eclipse.swt/EBWebView/ShaderCache/data_1 differ diff --git a/.metadata/.plugins/org.eclipse.swt/EBWebView/Variations b/.metadata/.plugins/org.eclipse.swt/EBWebView/Variations index 5d75f57..a157215 100644 --- a/.metadata/.plugins/org.eclipse.swt/EBWebView/Variations +++ b/.metadata/.plugins/org.eclipse.swt/EBWebView/Variations @@ -1 +1 @@ -{"user_experience_metrics.stability.exited_cleanly":false,"variations_crash_streak":1} \ No newline at end of file +{"user_experience_metrics.stability.exited_cleanly":false,"variations_crash_streak":0} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.terminal.view.ui/.executables/data.properties b/.metadata/.plugins/org.eclipse.terminal.view.ui/.executables/data.properties index ac452bc..eb7400d 100644 --- a/.metadata/.plugins/org.eclipse.terminal.view.ui/.executables/data.properties +++ b/.metadata/.plugins/org.eclipse.terminal.view.ui/.executables/data.properties @@ -1,5 +1,9 @@ -#Sun May 10 01:33:34 CEST 2026 +#Sun May 10 18:35:22 CEST 2026 0.Args=--login -i 0.Name=Git Bash 0.Path=C\:\\Program Files\\Git\\bin\\sh.exe 0.Translate=true +1.Args=--distribution Debian +1.Name=Debian (WSL) +1.Path=C\:\\Windows\\System32\\wsl.exe +1.Translate=true diff --git a/.metadata/version.ini b/.metadata/version.ini index 58fd960..b80b2b8 100644 --- a/.metadata/version.ini +++ b/.metadata/version.ini @@ -1,3 +1,3 @@ -#Sun May 10 01:31:23 CEST 2026 +#Sun May 10 18:29:39 CEST 2026 org.eclipse.core.runtime=2 org.eclipse.platform=4.39.0.v20260226-0420 diff --git a/asdl/bin/jcf_map/exercise/BinaryTreeMap.class b/asdl/bin/jcf_map/exercise/BinaryTreeMap.class new file mode 100644 index 0000000..4e0513b Binary files /dev/null and b/asdl/bin/jcf_map/exercise/BinaryTreeMap.class differ diff --git a/asdl/bin/jcf_map/exercise/CompanyMap.class b/asdl/bin/jcf_map/exercise/CompanyMap.class new file mode 100644 index 0000000..1463ff7 Binary files /dev/null and b/asdl/bin/jcf_map/exercise/CompanyMap.class differ diff --git a/asdl/bin/jcf_map/exercise/Dispari.class b/asdl/bin/jcf_map/exercise/Dispari.class new file mode 100644 index 0000000..c2b7cef Binary files /dev/null and b/asdl/bin/jcf_map/exercise/Dispari.class differ diff --git a/asdl/bin/list/mylinkedlist/MyLinkedList$Node.class b/asdl/bin/list/mylinkedlist/MyLinkedList$Node.class new file mode 100644 index 0000000..c7eb928 Binary files /dev/null and b/asdl/bin/list/mylinkedlist/MyLinkedList$Node.class differ diff --git a/asdl/bin/list/mylinkedlist/MyLinkedList.class b/asdl/bin/list/mylinkedlist/MyLinkedList.class new file mode 100644 index 0000000..388912d Binary files /dev/null and b/asdl/bin/list/mylinkedlist/MyLinkedList.class differ diff --git a/asdl/bin/parziale/p191108/Archivio.class b/asdl/bin/parziale/p191108/Archivio.class new file mode 100644 index 0000000..35b030a Binary files /dev/null and b/asdl/bin/parziale/p191108/Archivio.class differ diff --git a/asdl/bin/parziale/p191108/Cliente.class b/asdl/bin/parziale/p191108/Cliente.class new file mode 100644 index 0000000..363d3c8 Binary files /dev/null and b/asdl/bin/parziale/p191108/Cliente.class differ diff --git a/asdl/src/jcf_map/exercise/BinaryTreeMap.java b/asdl/src/jcf_map/exercise/BinaryTreeMap.java new file mode 100644 index 0000000..f4dd1fa --- /dev/null +++ b/asdl/src/jcf_map/exercise/BinaryTreeMap.java @@ -0,0 +1,43 @@ +package jcf_map.exercise; + +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 > int ContaDuplicati(BinaryNode node) { + int dup = 0; + TreeMap mappa = new TreeMap(); + ContaDuplicati(node, mappa); + + for (T element : mappa.keySet()) { + int occurrency = mappa.get(element); + if (occurrency > 1) dup++; + } + + return dup; + } + + protected static void ContaDuplicati(BinaryNode node, TreeMap 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); + } + +} diff --git a/asdl/src/jcf_map/exercise/CompanyMap.java b/asdl/src/jcf_map/exercise/CompanyMap.java new file mode 100644 index 0000000..8d9df17 --- /dev/null +++ b/asdl/src/jcf_map/exercise/CompanyMap.java @@ -0,0 +1,41 @@ +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 + */ + + public static void main(String[] main) { + new CompanyMap().run(); + } + + public void run() { + TreeMap mappaAssociazione = new TreeMap(); + + mappaAssociazione.put("Rossi Marco", 8); + mappaAssociazione.put("Bianchi Luca", 14); + mappaAssociazione.put("Esposito Andrea", 6); + mappaAssociazione.put("Ferrari Matteo", 6); + mappaAssociazione.put("Romano Giulia", 14); + mappaAssociazione.put("Ricci Alessia", 6); + + System.out.println("Stampa mappa associazione: "); + System.out.println(mappaAssociazione); + System.out.println("Numero di dipendenti: " + mappaAssociazione.size()); + } + +} diff --git a/asdl/src/jcf_map/exercise/Dispari.java b/asdl/src/jcf_map/exercise/Dispari.java new file mode 100644 index 0000000..009d0b4 --- /dev/null +++ b/asdl/src/jcf_map/exercise/Dispari.java @@ -0,0 +1,52 @@ +package jcf_map.exercise; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; + +public class Dispari> { + + /* + * 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 con una sola variabile di instanza private LinkedList 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 + */ + + // Variabile di instanza + private LinkedList myList = new LinkedList(); + + // Metodo di esercizio + public boolean isDispari() { + // Se la lista è vuota allora returna dispari + if (myList.isEmpty()) return true; + + // Contatore di elementi nella lista + HashMap valori = new HashMap(); + + Iterator 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; + } + + +} diff --git a/asdl/src/list/mylinkedlist/MyLinkedList.java b/asdl/src/list/mylinkedlist/MyLinkedList.java new file mode 100644 index 0000000..3137bcb --- /dev/null +++ b/asdl/src/list/mylinkedlist/MyLinkedList.java @@ -0,0 +1,183 @@ +package list.mylinkedlist; + +import java.util.List; +import java.util.NoSuchElementException; + +public class MyLinkedList implements List { + + /* + * Classe Nodo + */ + private static class Node { + + // Dati del nodo + private E data; + + // Riferimenti elemento precedente e successivo + private Node prev; + private Node next; + + // Costruttore + public Node(E data, Node prev, Node next) { + this.data = data; + this.prev = prev; + this.next = next; + } + + } + + private Node head = null; + private Node 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 newNode = new Node(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 firstPlace = head; + Node 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 lastNode = tail; + Node 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 newNode = new Node(item, null, null); + if (size == 0) { + head = tail = newNode; + } else { + Node 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 newNode = new Node(item, null, null); + + Node prevNode = head; + for (int i = 0; i < index - 1; i++) { + prevNode = prevNode.next; + } + + Node 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 currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + return currentNode.data; + } + + public E set(int index, E item) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + + Node currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + E oldData = currentNode.data; + currentNode.data = item; + return oldData; + } + + + +} diff --git a/asdl/src/parziale/p191108/Archivio.java b/asdl/src/parziale/p191108/Archivio.java new file mode 100644 index 0000000..6905b3f --- /dev/null +++ b/asdl/src/parziale/p191108/Archivio.java @@ -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 archivio = new HashMap(); + + // 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 creaArchivoPerCitta(String citta) { + if (citta == null) throw new NullPointerException(); + + HashMap autoSelezionate = new HashMap(); + + 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 dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili + public Map archivioOrdinatoRispettoAllaTarga() { + TreeMap archivioOrdinato = new TreeMap(archivio); + return archivioOrdinato; + } + + // 6. Stampa su video l'archivio attuale + public void stampaArchivio() { + System.out.println(archivio); + } + +} diff --git a/asdl/src/parziale/p191108/Cliente.java b/asdl/src/parziale/p191108/Cliente.java new file mode 100644 index 0000000..9854512 --- /dev/null +++ b/asdl/src/parziale/p191108/Cliente.java @@ -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); + } +}