diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e10e727 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.metadata/ diff --git a/.metadata/.lock_info b/.metadata/.lock_info index 0482fcb..82aab17 100644 --- a/.metadata/.lock_info +++ b/.metadata/.lock_info @@ -1,4 +1,4 @@ -#Tue May 12 13:59:16 CEST 2026 -host=DESKTOP-ALBQHEK -process-id=5624 +#Tue May 12 15:01:46 CEST 2026 +host=eslusarzs-MacBook-Pro.local +process-id=23862 user=eslusarz diff --git a/.metadata/.log b/.metadata/.log index 0c4da35..0e7da14 100644 --- a/.metadata/.log +++ b/.metadata/.log @@ -891,3 +891,171 @@ user global configuration and to define the default location to store repositori 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. +!SESSION 2026-05-12 15:01:42.681 ----------------------------------------------- +eclipse.buildId=4.39.0.20260305-0817 +java.version=21.0.10 +java.vendor=Eclipse Adoptium +BootLoader constants: OS=macosx, ARCH=aarch64, WS=cocoa, NL=it_IT +Framework arguments: -product org.eclipse.epp.package.java.product -keyring /Users/eslusarz/.eclipse_keyring +Command-line arguments: -os macosx -ws cocoa -arch aarch64 -product org.eclipse.epp.package.java.product -keyring /Users/eslusarz/.eclipse_keyring + +!ENTRY ch.qos.logback.classic 1 0 2026-05-12 15:01:43.636 +!MESSAGE Activated before the state location was initialized. Retry after the state location is initialized. + +!ENTRY org.eclipse.core.resources 2 10035 2026-05-12 15:01:46.121 +!MESSAGE The workspace exited with unsaved changes in the previous session; refreshing workspace to recover changes. + +!ENTRY org.eclipse.core.resources 4 567 2026-05-12 15:01:46.173 +!MESSAGE Workspace restored, but some problems occurred. +!SUBENTRY 1 org.eclipse.core.resources 4 567 2026-05-12 15:01:46.173 +!MESSAGE Could not read metadata for '.org.eclipse.egit.core.cmp'. +!STACK 1 +org.eclipse.core.internal.resources.ResourceException(/.org.eclipse.egit.core.cmp)[567]: java.lang.Exception: The project description file (.project) for '.org.eclipse.egit.core.cmp' is missing. This file contains important information about the project. The project will not function properly until this file is restored. + at org.eclipse.core.internal.resources.ResourceException.provideStackTrace(ResourceException.java:42) + at org.eclipse.core.internal.resources.ResourceException.(ResourceException.java:38) + at org.eclipse.core.internal.localstore.FileSystemResourceManager.read(FileSystemResourceManager.java:1015) + at org.eclipse.core.internal.resources.SaveManager.restoreMetaInfo(SaveManager.java:1004) + at org.eclipse.core.internal.resources.SaveManager.restoreMetaInfo(SaveManager.java:983) + at org.eclipse.core.internal.resources.SaveManager.restore(SaveManager.java:825) + at org.eclipse.core.internal.resources.SaveManager.startup(SaveManager.java:1689) + at org.eclipse.core.internal.resources.Workspace.startup(Workspace.java:2677) + at org.eclipse.core.internal.resources.Workspace.open(Workspace.java:2369) + at org.eclipse.core.resources.ResourcesPlugin$WorkspaceInitCustomizer.addingService(ResourcesPlugin.java:591) + at org.eclipse.core.resources.ResourcesPlugin$WorkspaceInitCustomizer.addingService(ResourcesPlugin.java:1) + at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:947) + at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:1) + at org.osgi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.java:257) + at org.osgi.util.tracker.AbstractTracked.trackInitial(AbstractTracked.java:184) + at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:324) + at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:267) + at org.eclipse.core.resources.ResourcesPlugin.start(ResourcesPlugin.java:565) + at org.eclipse.osgi.internal.framework.BundleContextImpl$2.run(BundleContextImpl.java:838) + at org.eclipse.osgi.internal.framework.BundleContextImpl$2.run(BundleContextImpl.java:1) + at java.base/java.security.AccessController.doPrivileged(AccessController.java:571) + at org.eclipse.osgi.internal.framework.BundleContextImpl.startActivator(BundleContextImpl.java:830) + at org.eclipse.osgi.internal.framework.BundleContextImpl.start(BundleContextImpl.java:780) + at org.eclipse.osgi.internal.framework.EquinoxBundle.startWorker0(EquinoxBundle.java:1082) + at org.eclipse.osgi.internal.framework.EquinoxBundle$EquinoxModule.startWorker(EquinoxBundle.java:394) + at org.eclipse.osgi.container.Module.doStart(Module.java:643) + at org.eclipse.osgi.container.Module.start(Module.java:500) + at org.eclipse.osgi.framework.util.SecureAction.start(SecureAction.java:528) + at org.eclipse.osgi.internal.hooks.EclipseLazyStarter.postFindLocalClass(EclipseLazyStarter.java:122) + at org.eclipse.osgi.internal.loader.classpath.ClasspathManager.findLocalClass(ClasspathManager.java:620) + at org.eclipse.osgi.internal.loader.ModuleClassLoader.findLocalClass(ModuleClassLoader.java:353) + at org.eclipse.osgi.internal.loader.BundleLoader.findLocalClass(BundleLoader.java:418) + at org.eclipse.osgi.internal.loader.sources.SingleSourcePackage.loadClass(SingleSourcePackage.java:41) + at org.eclipse.osgi.internal.loader.BundleLoader.findClass0(BundleLoader.java:523) + 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.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:186) + 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) +!SUBENTRY 2 org.eclipse.core.resources 4 567 2026-05-12 15:01:46.174 +!MESSAGE The project description file (.project) for '.org.eclipse.egit.core.cmp' is missing. This file contains important information about the project. The project will not function properly until this file is restored. +!STACK 0 +java.lang.Exception: The project description file (.project) for '.org.eclipse.egit.core.cmp' is missing. This file contains important information about the project. The project will not function properly until this file is restored. + at org.eclipse.core.internal.resources.ResourceException.provideStackTrace(ResourceException.java:42) + at org.eclipse.core.internal.resources.ResourceException.(ResourceException.java:38) + at org.eclipse.core.internal.localstore.FileSystemResourceManager.read(FileSystemResourceManager.java:1015) + at org.eclipse.core.internal.resources.SaveManager.restoreMetaInfo(SaveManager.java:1004) + at org.eclipse.core.internal.resources.SaveManager.restoreMetaInfo(SaveManager.java:983) + at org.eclipse.core.internal.resources.SaveManager.restore(SaveManager.java:825) + at org.eclipse.core.internal.resources.SaveManager.startup(SaveManager.java:1689) + at org.eclipse.core.internal.resources.Workspace.startup(Workspace.java:2677) + at org.eclipse.core.internal.resources.Workspace.open(Workspace.java:2369) + at org.eclipse.core.resources.ResourcesPlugin$WorkspaceInitCustomizer.addingService(ResourcesPlugin.java:591) + at org.eclipse.core.resources.ResourcesPlugin$WorkspaceInitCustomizer.addingService(ResourcesPlugin.java:1) + at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:947) + at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:1) + at org.osgi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.java:257) + at org.osgi.util.tracker.AbstractTracked.trackInitial(AbstractTracked.java:184) + at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:324) + at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:267) + at org.eclipse.core.resources.ResourcesPlugin.start(ResourcesPlugin.java:565) + at org.eclipse.osgi.internal.framework.BundleContextImpl$2.run(BundleContextImpl.java:838) + at org.eclipse.osgi.internal.framework.BundleContextImpl$2.run(BundleContextImpl.java:1) + at java.base/java.security.AccessController.doPrivileged(AccessController.java:571) + at org.eclipse.osgi.internal.framework.BundleContextImpl.startActivator(BundleContextImpl.java:830) + at org.eclipse.osgi.internal.framework.BundleContextImpl.start(BundleContextImpl.java:780) + at org.eclipse.osgi.internal.framework.EquinoxBundle.startWorker0(EquinoxBundle.java:1082) + at org.eclipse.osgi.internal.framework.EquinoxBundle$EquinoxModule.startWorker(EquinoxBundle.java:394) + at org.eclipse.osgi.container.Module.doStart(Module.java:643) + at org.eclipse.osgi.container.Module.start(Module.java:500) + at org.eclipse.osgi.framework.util.SecureAction.start(SecureAction.java:528) + at org.eclipse.osgi.internal.hooks.EclipseLazyStarter.postFindLocalClass(EclipseLazyStarter.java:122) + at org.eclipse.osgi.internal.loader.classpath.ClasspathManager.findLocalClass(ClasspathManager.java:620) + at org.eclipse.osgi.internal.loader.ModuleClassLoader.findLocalClass(ModuleClassLoader.java:353) + at org.eclipse.osgi.internal.loader.BundleLoader.findLocalClass(BundleLoader.java:418) + at org.eclipse.osgi.internal.loader.sources.SingleSourcePackage.loadClass(SingleSourcePackage.java:41) + at org.eclipse.osgi.internal.loader.BundleLoader.findClass0(BundleLoader.java:523) + 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.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:186) + 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 ch.qos.logback.classic 1 0 2026-05-12 15:01:46.409 +!MESSAGE Logback config file: /Users/eslusarz/eclipse-workspace/.metadata/.plugins/org.eclipse.m2e.logback/logback.2.7.101.20251017-1242.xml + +!ENTRY org.eclipse.jdt.ui 4 0 2026-05-13 12:09:02.056 +!MESSAGE AbortCompilation computing hover information in Graph.java at offset 199 +!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:363) + 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 network; + +public interface Graph implements Iterable { + + public boolean containsVertex(Vertex vertex); + + public boolean addVertex(Vertex vertex); + + public boolean |removeVertex(Vertex vertex); +----- + 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 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/0/b0421e9f5e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/0/b0421e9f5e490011162d85db97d6efcd deleted file mode 100644 index afce174..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/0/b0421e9f5e490011162d85db97d6efcd +++ /dev/null @@ -1,491 +0,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 i; - if () - - return n - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/401b103780490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1/401b103780490011162d85db97d6efcd deleted file mode 100644 index 28c340a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1/401b103780490011162d85db97d6efcd +++ /dev/null @@ -1,35 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - new InteriRipetuti().run(); - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } else System.out.println("Intero inserito: " + x); - } - - // Stampa di hashset - System.out.println("Hashset"); - for (Integer x : hashset) System.out.println(x + " "); - System.out.println(); System.out.println(hashset); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/11/7022f37f5e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/11/7022f37f5e490011162d85db97d6efcd deleted file mode 100644 index f4ade3c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/11/7022f37f5e490011162d85db97d6efcd +++ /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 i; - - if () - - return n - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/12/1031c6fa75490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/12/1031c6fa75490011162d85db97d6efcd deleted file mode 100644 index 1f13806..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/12/1031c6fa75490011162d85db97d6efcd +++ /dev/null @@ -1,30 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/12/70d119134b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/12/70d119134b490011162d85db97d6efcd deleted file mode 100644 index 65702fe..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/12/70d119134b490011162d85db97d6efcd +++ /dev/null @@ -1,17 +0,0 @@ -package binary_tree; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/12/c0e566217c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/12/c0e566217c490011162d85db97d6efcd deleted file mode 100644 index 8a079e6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/12/c0e566217c490011162d85db97d6efcd +++ /dev/null @@ -1,39 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - Collections.sort(giochi); - - - - } - - private void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/15/609e82a97f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/15/609e82a97f490011162d85db97d6efcd deleted file mode 100644 index c4b00ab..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/15/609e82a97f490011162d85db97d6efcd +++ /dev/null @@ -1,30 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - Syste - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/17/b0ab36db5f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/17/b0ab36db5f490011162d85db97d6efcd deleted file mode 100644 index 4a8eed4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/17/b0ab36db5f490011162d85db97d6efcd +++ /dev/null @@ -1,494 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/19/e07eb6514d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/19/e07eb6514d490011162d85db97d6efcd deleted file mode 100644 index 807e8dc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/19/e07eb6514d490011162d85db97d6efcd +++ /dev/null @@ -1,48 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - public static Integer maxValue(BinaryTree btree) { - Iterator iterator = btree.iteratorInOrder(); - Integer maxValue; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1a/5001537d4c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1a/5001537d4c490011162d85db97d6efcd deleted file mode 100644 index cf5fb22..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1a/5001537d4c490011162d85db97d6efcd +++ /dev/null @@ -1,31 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if (!(current % 2 == 0)) even = false; - } - return even; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/3006a2397f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/3006a2397f490011162d85db97d6efcd deleted file mode 100644 index b9a9895..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/3006a2397f490011162d85db97d6efcd +++ /dev/null @@ -1,16 +0,0 @@ -package jcf_set.example; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/40d7bbfe5f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/40d7bbfe5f490011162d85db97d6efcd deleted file mode 100644 index 2d90c3c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/40d7bbfe5f490011162d85db97d6efcd +++ /dev/null @@ -1,500 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1d/a0be4c3060490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1d/a0be4c3060490011162d85db97d6efcd deleted file mode 100644 index cc6d380..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1d/a0be4c3060490011162d85db97d6efcd +++ /dev/null @@ -1,502 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/101c63307c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/101c63307c490011162d85db97d6efcd deleted file mode 100644 index b1ef8bd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/101c63307c490011162d85db97d6efcd +++ /dev/null @@ -1,39 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - Collections.sort(giochi); - printList(giochi); - - - } - - private void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/20d762397b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/20d762397b490011162d85db97d6efcd deleted file mode 100644 index 68ea4aa..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/20d762397b490011162d85db97d6efcd +++ /dev/null @@ -1,26 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - Collections.sort(giochi); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/805daf257f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/805daf257f490011162d85db97d6efcd deleted file mode 100644 index 1d2d4e2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/805daf257f490011162d85db97d6efcd +++ /dev/null @@ -1,16 +0,0 @@ -package jcf_set.example; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1f/40f143b075490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1f/40f143b075490011162d85db97d6efcd deleted file mode 100644 index 7c3bfa4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1f/40f143b075490011162d85db97d6efcd +++ /dev/null @@ -1,17 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/21/206ca7aa7b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/21/206ca7aa7b490011162d85db97d6efcd deleted file mode 100644 index 22bde1c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/21/206ca7aa7b490011162d85db97d6efcd +++ /dev/null @@ -1,36 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - Collections.sort(giochi); - - - - } - - private void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/23/90d9521d7b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/23/90d9521d7b490011162d85db97d6efcd deleted file mode 100644 index 009c5c4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/23/90d9521d7b490011162d85db97d6efcd +++ /dev/null @@ -1,26 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collection; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - List.sort(giochi); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/25/a046cf4978490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/25/a046cf4978490011162d85db97d6efcd deleted file mode 100644 index d4d46cc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/25/a046cf4978490011162d85db97d6efcd +++ /dev/null @@ -1,18 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco3 implements Comparator { - - /** - * Prima ordina per piattaforma alfabetica. - * Se la piattaforma è uguale, per anno - * di uscita crescente - * se l'anno è uguale, allora per titolo alfabetico - */ - @Override - public int compare(Videogioco v1, Videogioco v2) { - int cpm = - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/27/40b8d14b76490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/27/40b8d14b76490011162d85db97d6efcd deleted file mode 100644 index 9f5e327..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/27/40b8d14b76490011162d85db97d6efcd +++ /dev/null @@ -1,55 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } - - public Integer annoUscita() { - return annoUscita; - } - - public Double prezzo() { - return prezzo; - } - - public Integer voto() { - return voto; - } - - // Implementazione del metodo comparatore - - /** - * compareTo ordina per prezzo crescente - */ -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/27/a08d520d63490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/27/a08d520d63490011162d85db97d6efcd deleted file mode 100644 index de2492c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/27/a08d520d63490011162d85db97d6efcd +++ /dev/null @@ -1,515 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - BinaryNode copy; - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - copy = new BinaryNode(node.getRight().getData()); - node.setLeft(copy); - copy.setParentAsLeftChild(node); - } else { - copy = new BinaryNode(node.getLeft().getData()); - node.setRight(copy); - copy.setParentAsRightChild(node); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/28/f07f62ee75490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/28/f07f62ee75490011162d85db97d6efcd deleted file mode 100644 index bb83a34..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/28/f07f62ee75490011162d85db97d6efcd +++ /dev/null @@ -1,28 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2a/d0a5603a80490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/d0a5603a80490011162d85db97d6efcd deleted file mode 100644 index 811fdba..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2a/d0a5603a80490011162d85db97d6efcd +++ /dev/null @@ -1,37 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - new InteriRipetuti().run(); - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } else System.out.println("Intero inserito: " + x); - } - - // Stampa di hashset - System.out.println("Hashset"); - for (Integer x : hashset) System.out.println(x + " "); - System.out.println(); System.out.println(hashset); - - // TREESET (ordinato) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2b/20551faf7a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/20551faf7a490011162d85db97d6efcd deleted file mode 100644 index e772795..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2b/20551faf7a490011162d85db97d6efcd +++ /dev/null @@ -1,9 +0,0 @@ -package comparatori.videogioco; - -public class Main { - - public static void main(String[] Args) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2b/804ca40980490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/804ca40980490011162d85db97d6efcd deleted file mode 100644 index c24a1eb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2b/804ca40980490011162d85db97d6efcd +++ /dev/null @@ -1,34 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } else System.out.println("Intero inserito: " + x); - } - - // Stampa di hashset - System.out.println("Hashset"); - for (Integer x : hashset) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2c/e034ebef4b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/e034ebef4b490011162d85db97d6efcd deleted file mode 100644 index 52e06c9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2c/e034ebef4b490011162d85db97d6efcd +++ /dev/null @@ -1,27 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && ) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2d/0069cdb27a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/0069cdb27a490011162d85db97d6efcd deleted file mode 100644 index 8e8db03..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2d/0069cdb27a490011162d85db97d6efcd +++ /dev/null @@ -1,11 +0,0 @@ -package comparatori.videogioco; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2e/b0be493c60490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/b0be493c60490011162d85db97d6efcd deleted file mode 100644 index 8592580..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2e/b0be493c60490011162d85db97d6efcd +++ /dev/null @@ -1,503 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3/b0f262ea5f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3/b0f262ea5f490011162d85db97d6efcd deleted file mode 100644 index 7941d03..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3/b0f262ea5f490011162d85db97d6efcd +++ /dev/null @@ -1,500 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - - } - - public void makeFull(BinaryNode giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - System.out.println("ordina per prezzo crescente"); - Collections.sort(giochi); - printList(giochi); - - // Ordina i videogiochi per titolo alfabetico crescente - System.out.println("Ordina i videogiochi per titolo alfabetico crescente"); - Collections.sort(giochi, new ComparatorVideogioco1()); - printList(giochi); - - // Ordina dal voto più alto al più basso - System.out.println("Ordina dal voto più alto al più basso"); - Collections.sort(giochi, new ComparatorVideogioco1()); - printList(giochi); - - } - - private static void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/b0face3276490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/30/b0face3276490011162d85db97d6efcd deleted file mode 100644 index b994ee6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/30/b0face3276490011162d85db97d6efcd +++ /dev/null @@ -1,51 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } - - public Integer annoUscita() { - return annoUscita; - } - - public Double prezzo() { - return prezzo; - } - - public Integer voto() { - return voto; - } - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/d05aa57776490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/30/d05aa57776490011162d85db97d6efcd deleted file mode 100644 index b5f28f0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/30/d05aa57776490011162d85db97d6efcd +++ /dev/null @@ -1,63 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } - - public Integer annoUscita() { - return annoUscita; - } - - public Double prezzo() { - return prezzo; - } - - public Integer voto() { - return voto; - } - - // Implementazione del metodo comparatore - - /** - * compareTo ordina per prezzo crescente - */ - @Override - public int compareTo(Videogioco videogioco) { - - if (this.prezzo < videogioco.prezzo) return -1; - if (this.prezzo > videogioco.prezzo) return 1; - return 0; - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/32/e05232e17c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/32/e05232e17c490011162d85db97d6efcd deleted file mode 100644 index 933752d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/32/e05232e17c490011162d85db97d6efcd +++ /dev/null @@ -1,41 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - Collections.sort(giochi); - printList(giochi); - - // Ordina i videogiochi per titolo alfabetico crescente - - - } - - private static void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/33/708568527c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/33/708568527c490011162d85db97d6efcd deleted file mode 100644 index 0824a88..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/33/708568527c490011162d85db97d6efcd +++ /dev/null @@ -1,41 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - Collections.sort(giochi); - printList(giochi); - - // - - - } - - private static void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/34/204f00d87b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/34/204f00d87b490011162d85db97d6efcd deleted file mode 100644 index a43a0bb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/34/204f00d87b490011162d85db97d6efcd +++ /dev/null @@ -1,61 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } - - public Integer annoUscita() { - return annoUscita; - } - - public Double prezzo() { - return prezzo; - } - - public Integer voto() { - return voto; - } - - // Implementazione del metodo comparatore - - /** - * compareTo ordina per prezzo crescente - */ - @Override - public int compareTo(Videogioco videogioco) { - if (this.prezzo < videogioco.prezzo) return -1; - if (this.prezzo > videogioco.prezzo) return 1; - return 0; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/35/60bf840461490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/35/60bf840461490011162d85db97d6efcd deleted file mode 100644 index bc16a9a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/35/60bf840461490011162d85db97d6efcd +++ /dev/null @@ -1,511 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - BinaryNode copy; - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - copy = - } else { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/37/70952f4a80490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/37/70952f4a80490011162d85db97d6efcd deleted file mode 100644 index 17bebbd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/37/70952f4a80490011162d85db97d6efcd +++ /dev/null @@ -1,38 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - new InteriRipetuti().run(); - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET (non ordinato) - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } else System.out.println("Intero inserito: " + x); - } - - // Stampa di hashset - System.out.println("Hashset"); - for (Integer x : hashset) System.out.println(x + " "); - System.out.println(); System.out.println(hashset); - - // TREESET (ordinato) - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/37/b021defa7c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/37/b021defa7c490011162d85db97d6efcd deleted file mode 100644 index c6ab412..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/37/b021defa7c490011162d85db97d6efcd +++ /dev/null @@ -1,43 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - System.out.println("ordina per prezzo crescente"); - Collections.sort(giochi); - printList(giochi); - - // Ordina i videogiochi per titolo alfabetico crescente - Collections.sort(giochi, new ComparatorVideogioco1()); - printList(giochi); - - } - - private static void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/37/d0436add7b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/37/d0436add7b490011162d85db97d6efcd deleted file mode 100644 index 36c029f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/37/d0436add7b490011162d85db97d6efcd +++ /dev/null @@ -1,64 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } - - public Integer annoUscita() { - return annoUscita; - } - - public Double prezzo() { - return prezzo; - } - - public Integer voto() { - return voto; - } - - // Implementazione del metodo comparatore - - /** - * compareTo ordina per prezzo crescente - */ - @Override - public int compareTo(Videogioco videogioco) { - if (this.prezzo < videogioco.prezzo) return -1; - if (this.prezzo > videogioco.prezzo) return 1; - return 0; - } - - @Override - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/39/b0015c7c4d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/39/b0015c7c4d490011162d85db97d6efcd deleted file mode 100644 index 5ca3e69..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/39/b0015c7c4d490011162d85db97d6efcd +++ /dev/null @@ -1,51 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - public static Integer maxValue(BinaryTree btree) { - Iterator iterator = btree.iteratorInOrder(); - if (!iterator.hasNext()) return null; - Integer maxValue = iterator.next(); - while (iterator.hasNext()) { - Integer current = iterator.next(); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/39/e08f03e560490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/39/e08f03e560490011162d85db97d6efcd deleted file mode 100644 index 5c953ce..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/39/e08f03e560490011162d85db97d6efcd +++ /dev/null @@ -1,511 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - BinaryNode copy; - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - - } else { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/0045265d4b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/0045265d4b490011162d85db97d6efcd deleted file mode 100644 index 981e4cc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/0045265d4b490011162d85db97d6efcd +++ /dev/null @@ -1,18 +0,0 @@ -package binary_tree; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/308204547c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/308204547c490011162d85db97d6efcd deleted file mode 100644 index 933752d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/308204547c490011162d85db97d6efcd +++ /dev/null @@ -1,41 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - Collections.sort(giochi); - printList(giochi); - - // Ordina i videogiochi per titolo alfabetico crescente - - - } - - private static void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/80cf170976490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/80cf170976490011162d85db97d6efcd deleted file mode 100644 index d2979c5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/80cf170976490011162d85db97d6efcd +++ /dev/null @@ -1,33 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3e/f0c6375075490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3e/f0c6375075490011162d85db97d6efcd deleted file mode 100644 index e69de29..0000000 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3f/50df0d247b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3f/50df0d247b490011162d85db97d6efcd deleted file mode 100644 index 58fb333..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3f/50df0d247b490011162d85db97d6efcd +++ /dev/null @@ -1,26 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - Collections.sort(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4/20401eed75490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4/20401eed75490011162d85db97d6efcd deleted file mode 100644 index 6afbb30..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4/20401eed75490011162d85db97d6efcd +++ /dev/null @@ -1,27 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4/30f44fa55e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4/30f44fa55e490011162d85db97d6efcd deleted file mode 100644 index 167ff74..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4/30f44fa55e490011162d85db97d6efcd +++ /dev/null @@ -1,491 +0,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 i; - if (node.getLeft() == null && node.getRight() == null) return 1; - - return n - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4/b04e3f9d4c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4/b04e3f9d4c490011162d85db97d6efcd deleted file mode 100644 index e970026..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4/b04e3f9d4c490011162d85db97d6efcd +++ /dev/null @@ -1,31 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/44/d0674f197d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/44/d0674f197d490011162d85db97d6efcd deleted file mode 100644 index 0d89af4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/44/d0674f197d490011162d85db97d6efcd +++ /dev/null @@ -1,51 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - System.out.println("ordina per prezzo crescente"); - Collections.sort(giochi); - printList(giochi); - - // Ordina i videogiochi per titolo alfabetico crescente - System.out.println("Ordina i videogiochi per titolo alfabetico crescente"); - Collections.sort(giochi, new ComparatorVideogioco1()); - printList(giochi); - - // Ordina dal voto più alto al più basso - System.out.println("Ordina dal voto più alto al più basso"); - Collections.sort(giochi, new ComparatorVideogioco2()); - printList(giochi); - - // Multi - - } - - private static void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/44/f011dd6380490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/44/f011dd6380490011162d85db97d6efcd deleted file mode 100644 index b7437e4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/44/f011dd6380490011162d85db97d6efcd +++ /dev/null @@ -1,44 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; -import java.util.TreeSet; - -public class InteriRipetuti { - - public static void main(String[] Args) { - new InteriRipetuti().run(); - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET (non ordinato) - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } else System.out.println("Intero inserito: " + x); - } - - // Stampa di hashset - System.out.println("Hashset"); - for (Integer x : hashset) System.out.println(x + " "); - System.out.println(); System.out.println(hashset); - - // TREESET (ordinato) - Set treeset = new TreeSet(hashset); - - // Stampa di treeset - System.out.println("Treeset"); - for (Integer x : treeset) System.out.println(x + " "); - System.out.println(); System.out.println(hashset); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/46/90c9ca2380490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/46/90c9ca2380490011162d85db97d6efcd deleted file mode 100644 index 6290f1e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/46/90c9ca2380490011162d85db97d6efcd +++ /dev/null @@ -1,35 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } else System.out.println("Intero inserito: " + x); - } - - // Stampa di hashset - System.out.println("Hashset"); - for (Integer x : hashset) System.out.println(x + " "); - System.out.println(); System.out.println(hashset); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/47/006e9fb77b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/47/006e9fb77b490011162d85db97d6efcd deleted file mode 100644 index b9a17dd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/47/006e9fb77b490011162d85db97d6efcd +++ /dev/null @@ -1,37 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - Collections.sort(giochi); - - - - } - - private void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/47/70cdcd744d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/47/70cdcd744d490011162d85db97d6efcd deleted file mode 100644 index 0981778..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/47/70cdcd744d490011162d85db97d6efcd +++ /dev/null @@ -1,51 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - public static Integer maxValue(BinaryTree btree) { - Iterator iterator = btree.iteratorInOrder(); - if (!iterator.hasNext()) return null; - Integer maxValue = iterator.next(); - while (iterator.hasNext()) { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/48/40bd1e004d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/48/40bd1e004d490011162d85db97d6efcd deleted file mode 100644 index 77df6f7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/48/40bd1e004d490011162d85db97d6efcd +++ /dev/null @@ -1,38 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/49/40eab8a977490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/49/40eab8a977490011162d85db97d6efcd deleted file mode 100644 index fb9303c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/49/40eab8a977490011162d85db97d6efcd +++ /dev/null @@ -1,11 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco2 implements Comparator { - - public in compare() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/49/70b7c91463490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/49/70b7c91463490011162d85db97d6efcd deleted file mode 100644 index d2ddbf7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/49/70b7c91463490011162d85db97d6efcd +++ /dev/null @@ -1,514 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - BinaryNode copy; - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - copy = new BinaryNode(node.getRight().getData()); - node.setLeft(copy); - copy.setParentAsLeftChild(node); - } else { - copy = new BinaryNode(node.getLeft().getData()); - node.setRight(copy); - copy.setParentAsRightChild(node); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/49/f0c531e67c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/49/f0c531e67c490011162d85db97d6efcd deleted file mode 100644 index e7a2989..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/49/f0c531e67c490011162d85db97d6efcd +++ /dev/null @@ -1,41 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - Collections.sort(giochi); - printList(giochi); - - // Ordina i videogiochi per titolo alfabetico crescente - Collections.sort(giochi, new ComparatorVideogioco1()); - - } - - private static void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4a/b00e859a62490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4a/b00e859a62490011162d85db97d6efcd deleted file mode 100644 index 10b0213..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4a/b00e859a62490011162d85db97d6efcd +++ /dev/null @@ -1,512 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - BinaryNode copy; - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - copy = new BinaryNode(node.getRight().getData()); - node.setLeft(copy); - } else { - copy = new BinaryNode(node.getLeft().getData()); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4b/5017771d63490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/5017771d63490011162d85db97d6efcd deleted file mode 100644 index a8da0b4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4b/5017771d63490011162d85db97d6efcd +++ /dev/null @@ -1,516 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - - - BinaryNode copy; - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - copy = new BinaryNode(node.getRight().getData()); - node.setLeft(copy); - copy.setParentAsLeftChild(node); - } else { - copy = new BinaryNode(node.getLeft().getData()); - node.setRight(copy); - copy.setParentAsRightChild(node); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4c/00101cf57e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4c/00101cf57e490011162d85db97d6efcd deleted file mode 100644 index e69de29..0000000 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4e/20e0276b76490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4e/20e0276b76490011162d85db97d6efcd deleted file mode 100644 index 5a1e610..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4e/20e0276b76490011162d85db97d6efcd +++ /dev/null @@ -1,59 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } - - public Integer annoUscita() { - return annoUscita; - } - - public Double prezzo() { - return prezzo; - } - - public Integer voto() { - return voto; - } - - // Implementazione del metodo comparatore - - /** - * compareTo ordina per prezzo crescente - */ - @Override - public int compareTo(Videogioco videogioco) { - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/d047e3ca75490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/d047e3ca75490011162d85db97d6efcd deleted file mode 100644 index f6fd5c5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/d047e3ca75490011162d85db97d6efcd +++ /dev/null @@ -1,24 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/52/e05461a15e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/52/e05461a15e490011162d85db97d6efcd deleted file mode 100644 index 9b037a5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/52/e05461a15e490011162d85db97d6efcd +++ /dev/null @@ -1,491 +0,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 i; - if (node.getLeft() == null && node.getRight()) return 1; - - return n - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/53/50f0698377490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/53/50f0698377490011162d85db97d6efcd deleted file mode 100644 index 86e3f74..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/53/50f0698377490011162d85db97d6efcd +++ /dev/null @@ -1,15 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco1 implements Comparator { - - /** - * Ordina i videogiochi per titolo alfabetico crescente - */ - @Override - public int compareTo(Videogioco v1, Videogioco v2) { - return String.compareTo(v1.titolo(), v2.titolo()); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/53/800056df76490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/53/800056df76490011162d85db97d6efcd deleted file mode 100644 index e69de29..0000000 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/53/c05369904c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/53/c05369904c490011162d85db97d6efcd deleted file mode 100644 index 7bd3163..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/53/c05369904c490011162d85db97d6efcd +++ /dev/null @@ -1,31 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current < 0) || !(current % 2 == 0)) even = false; - } - return even; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/54/909c1ce260490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/54/909c1ce260490011162d85db97d6efcd deleted file mode 100644 index 27fde3a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/54/909c1ce260490011162d85db97d6efcd +++ /dev/null @@ -1,509 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - - } else { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/57/10b14d217f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/57/10b14d217f490011162d85db97d6efcd deleted file mode 100644 index 551c4d6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/57/10b14d217f490011162d85db97d6efcd +++ /dev/null @@ -1,14 +0,0 @@ -package jcf_set.example; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/57/70f9172d60490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/57/70f9172d60490011162d85db97d6efcd deleted file mode 100644 index 81c71c2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/57/70f9172d60490011162d85db97d6efcd +++ /dev/null @@ -1,501 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; - if (node.hasLeft() && node.hasRight()) return; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/59/306a49074c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/59/306a49074c490011162d85db97d6efcd deleted file mode 100644 index ccde0e1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/59/306a49074c490011162d85db97d6efcd +++ /dev/null @@ -1,30 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if () - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/59/80fb7fa577490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/59/80fb7fa577490011162d85db97d6efcd deleted file mode 100644 index 55a14e2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/59/80fb7fa577490011162d85db97d6efcd +++ /dev/null @@ -1,5 +0,0 @@ -package comparatori.videogioco; - -public class ComparatorVideogioco2 implements Comparator { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/30fe91715f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/30fe91715f490011162d85db97d6efcd deleted file mode 100644 index 085dd13..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/30fe91715f490011162d85db97d6efcd +++ /dev/null @@ -1,493 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/c0c92f247c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/c0c92f247c490011162d85db97d6efcd deleted file mode 100644 index 5c4b7d2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/c0c92f247c490011162d85db97d6efcd +++ /dev/null @@ -1,39 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo - Collections.sort(giochi); - - - - } - - private void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/e0db09db4a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/e0db09db4a490011162d85db97d6efcd deleted file mode 100644 index d183c4f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/e0db09db4a490011162d85db97d6efcd +++ /dev/null @@ -1,14 +0,0 @@ -package binary_tree; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/205b16fa4b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/205b16fa4b490011162d85db97d6efcd deleted file mode 100644 index 084cb7d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/205b16fa4b490011162d85db97d6efcd +++ /dev/null @@ -1,29 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/60546b564d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/60546b564d490011162d85db97d6efcd deleted file mode 100644 index 5e3f3b1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/60546b564d490011162d85db97d6efcd +++ /dev/null @@ -1,49 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - public static Integer maxValue(BinaryTree btree) { - Iterator iterator = btree.iteratorInOrder(); - if (!iterator.hasNext()) return null; - Integer maxValue; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/c05d13224d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/c05d13224d490011162d85db97d6efcd deleted file mode 100644 index 49ea5c1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/c05d13224d490011162d85db97d6efcd +++ /dev/null @@ -1,43 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/20e4ce207f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/20e4ce207f490011162d85db97d6efcd deleted file mode 100644 index 52f7d8b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/20e4ce207f490011162d85db97d6efcd +++ /dev/null @@ -1,9 +0,0 @@ -package jcf_set.example; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/f0b3635e80490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/f0b3635e80490011162d85db97d6efcd deleted file mode 100644 index 15b1c17..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/f0b3635e80490011162d85db97d6efcd +++ /dev/null @@ -1,42 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; -import java.util.TreeSet; - -public class InteriRipetuti { - - public static void main(String[] Args) { - new InteriRipetuti().run(); - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET (non ordinato) - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } else System.out.println("Intero inserito: " + x); - } - - // Stampa di hashset - System.out.println("Hashset"); - for (Integer x : hashset) System.out.println(x + " "); - System.out.println(); System.out.println(hashset); - - // TREESET (ordinato) - Set treeset = new TreeSet(hashset); - - // Stampa di treeset - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/30c39e5478490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/30c39e5478490011162d85db97d6efcd deleted file mode 100644 index 5b1ccd7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/30c39e5478490011162d85db97d6efcd +++ /dev/null @@ -1,19 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco3 implements Comparator { - - /** - * Prima ordina per piattaforma alfabetica. - * Se la piattaforma è uguale, per anno - * di uscita crescente - * se l'anno è uguale, allora per titolo alfabetico - */ - @Override - public int compare(Videogioco v1, Videogioco v2) { - int cmp = v1.piattaforma().compareTo(v2.piattaforma()); - if (cmp == 0) return cmp; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/61/6043fa2a7d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/61/6043fa2a7d490011162d85db97d6efcd deleted file mode 100644 index 534320c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/61/6043fa2a7d490011162d85db97d6efcd +++ /dev/null @@ -1,54 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - System.out.println("ordina per prezzo crescente"); - Collections.sort(giochi); - printList(giochi); - - // Ordina i videogiochi per titolo alfabetico crescente - System.out.println("Ordina i videogiochi per titolo alfabetico crescente"); - Collections.sort(giochi, new ComparatorVideogioco1()); - printList(giochi); - - // Ordina dal voto più alto al più basso - System.out.println("Ordina dal voto più alto al più basso"); - Collections.sort(giochi, new ComparatorVideogioco2()); - printList(giochi); - - // Multi - System.out.println("Multi"); - Collections.sort(giochi, new ComparatorVideogioco3()); - printList(giochi); - - } - - private static void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/63/e050cca14c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/63/e050cca14c490011162d85db97d6efcd deleted file mode 100644 index 21d53cb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/63/e050cca14c490011162d85db97d6efcd +++ /dev/null @@ -1,33 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/64/a0def40a4f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/64/a0def40a4f490011162d85db97d6efcd deleted file mode 100644 index a268874..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/64/a0def40a4f490011162d85db97d6efcd +++ /dev/null @@ -1,62 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - public static Integer maxValue(BinaryTree btree) { - Iterator iterator = btree.iteratorInOrder(); - if (!iterator.hasNext()) return null; - Integer maxValue = iterator.next(); - while (iterator.hasNext()) { - Integer current = iterator.next(); - if (current > maxValue) maxValue = current; - } - return maxValue; - } - - /* - * 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/65/10c1a53477490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/65/10c1a53477490011162d85db97d6efcd deleted file mode 100644 index ac499e3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/65/10c1a53477490011162d85db97d6efcd +++ /dev/null @@ -1,15 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco1 implements Comparator { - - /** - * Ordina i videogiochi per titolo alfabetico crescente - */ - @Override - public int compareTo(Videogioco v1, Videogioco v2) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/66/104863334d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/66/104863334d490011162d85db97d6efcd deleted file mode 100644 index 0f91771..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/66/104863334d490011162d85db97d6efcd +++ /dev/null @@ -1,46 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - public static Integer maxValue(BinaryTree btree) { - Integer maxValue; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/66/30ab513061490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/66/30ab513061490011162d85db97d6efcd deleted file mode 100644 index 39849e7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/66/30ab513061490011162d85db97d6efcd +++ /dev/null @@ -1,511 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - BinaryNode copy; - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - copy = new BinaryNode(node.getRight()); - } else { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/67/805ea56f61490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/67/805ea56f61490011162d85db97d6efcd deleted file mode 100644 index f0ab9d5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/67/805ea56f61490011162d85db97d6efcd +++ /dev/null @@ -1,511 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - BinaryNode copy; - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - copy = new BinaryNode(); - } else { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/68/0045007978490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/68/0045007978490011162d85db97d6efcd deleted file mode 100644 index 35e7c47..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/68/0045007978490011162d85db97d6efcd +++ /dev/null @@ -1,20 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco3 implements Comparator { - - /** - * Prima ordina per piattaforma alfabetica. - * Se la piattaforma è uguale, per anno - * di uscita crescente - * se l'anno è uguale, allora per titolo alfabetico - */ - @Override - public int compare(Videogioco v1, Videogioco v2) { - int cmp = v1.piattaforma().compareTo(v2.piattaforma()); - if (cmp == 0) return cmp; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/69/b0ff378178490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/69/b0ff378178490011162d85db97d6efcd deleted file mode 100644 index e77e864..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/69/b0ff378178490011162d85db97d6efcd +++ /dev/null @@ -1,21 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco3 implements Comparator { - - /** - * Prima ordina per piattaforma alfabetica. - * Se la piattaforma è uguale, per anno - * di uscita crescente - * se l'anno è uguale, allora per titolo alfabetico - */ - @Override - public int compare(Videogioco v1, Videogioco v2) { - int cmp = v1.piattaforma().compareTo(v2.piattaforma()); - if (cmp == 0) return cmp; - cmp = v1.annoUscita() - v2.annoUscita(); - if (cmp == 0) return cmp; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/30b3802076490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/30b3802076490011162d85db97d6efcd deleted file mode 100644 index 763cecd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/30b3802076490011162d85db97d6efcd +++ /dev/null @@ -1,41 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } - - public Integer annoUscita() { - return annoUscita; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/40fb16d460490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/40fb16d460490011162d85db97d6efcd deleted file mode 100644 index aeee988..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/40fb16d460490011162d85db97d6efcd +++ /dev/null @@ -1,506 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - if (!node.hasLeft()) { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6b/e08ea0f94b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6b/e08ea0f94b490011162d85db97d6efcd deleted file mode 100644 index 0354789..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6b/e08ea0f94b490011162d85db97d6efcd +++ /dev/null @@ -1,29 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6f/60faf8b460490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6f/60faf8b460490011162d85db97d6efcd deleted file mode 100644 index f257a41..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6f/60faf8b460490011162d85db97d6efcd +++ /dev/null @@ -1,504 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - //if () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6f/a0a27c0761490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6f/a0a27c0761490011162d85db97d6efcd deleted file mode 100644 index f0ab9d5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6f/a0a27c0761490011162d85db97d6efcd +++ /dev/null @@ -1,511 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - BinaryNode copy; - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - copy = new BinaryNode(); - } else { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/70/60aed3d277490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/70/60aed3d277490011162d85db97d6efcd deleted file mode 100644 index 6b24d05..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/70/60aed3d277490011162d85db97d6efcd +++ /dev/null @@ -1,16 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco2 implements Comparator { - - /** - * Ordina dal più alto al più basso - * @return - */ - @Override - public int compare() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/71/70864afd7f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/71/70864afd7f490011162d85db97d6efcd deleted file mode 100644 index a88b058..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/71/70864afd7f490011162d85db97d6efcd +++ /dev/null @@ -1,33 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } else System.out.println("Intero inserito: " + x); - } - - // Stampa di hashset - System.out.println("Hashset"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/71/e0a180047b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/71/e0a180047b490011162d85db97d6efcd deleted file mode 100644 index f80a5ec..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/71/e0a180047b490011162d85db97d6efcd +++ /dev/null @@ -1,25 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - Collection.sort(giochi); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/72/60234d1380490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/72/60234d1380490011162d85db97d6efcd deleted file mode 100644 index 846e17f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/72/60234d1380490011162d85db97d6efcd +++ /dev/null @@ -1,34 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } else System.out.println("Intero inserito: " + x); - } - - // Stampa di hashset - System.out.println("Hashset"); - for (Integer x : hashset) System.out.println(x + " "); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/72/70cc29e77a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/72/70cc29e77a490011162d85db97d6efcd deleted file mode 100644 index 1af4861..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/72/70cc29e77a490011162d85db97d6efcd +++ /dev/null @@ -1,24 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/74/d03a010e7c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/74/d03a010e7c490011162d85db97d6efcd deleted file mode 100644 index 9e3c2e8..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/74/d03a010e7c490011162d85db97d6efcd +++ /dev/null @@ -1,66 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } - - public Integer annoUscita() { - return annoUscita; - } - - public Double prezzo() { - return prezzo; - } - - public Integer voto() { - return voto; - } - - // Implementazione del metodo comparatore - - /** - * compareTo ordina per prezzo crescente - */ - @Override - public int compareTo(Videogioco videogioco) { - if (this.prezzo < videogioco.prezzo) return -1; - if (this.prezzo > videogioco.prezzo) return 1; - return 0; - } - - @Override - public String toString() { - return - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/003d6b9e77490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/77/003d6b9e77490011162d85db97d6efcd deleted file mode 100644 index b97d3e1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/77/003d6b9e77490011162d85db97d6efcd +++ /dev/null @@ -1,5 +0,0 @@ -package comparatori.videogioco; - -public class ComparatorVideogioco2 { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/60f2f1c17b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/77/60f2f1c17b490011162d85db97d6efcd deleted file mode 100644 index c4e46ba..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/77/60f2f1c17b490011162d85db97d6efcd +++ /dev/null @@ -1,39 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - Collections.sort(giochi); - - - - } - - private void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print("["); - System.out.print("["); - System.out.print("]"); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7a/306fed7160490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7a/306fed7160490011162d85db97d6efcd deleted file mode 100644 index da62ad6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7a/306fed7160490011162d85db97d6efcd +++ /dev/null @@ -1,503 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - - if () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7d/f0675fe07b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7d/f0675fe07b490011162d85db97d6efcd deleted file mode 100644 index d8d3586..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7d/f0675fe07b490011162d85db97d6efcd +++ /dev/null @@ -1,66 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } - - public Integer annoUscita() { - return annoUscita; - } - - public Double prezzo() { - return prezzo; - } - - public Integer voto() { - return voto; - } - - // Implementazione del metodo comparatore - - /** - * compareTo ordina per prezzo crescente - */ - @Override - public int compareTo(Videogioco videogioco) { - if (this.prezzo < videogioco.prezzo) return -1; - if (this.prezzo > videogioco.prezzo) return 1; - return 0; - } - - @Override - public int toString() { - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/80/d0a8189e7a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/80/d0a8189e7a490011162d85db97d6efcd deleted file mode 100644 index 6f3c3f2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/80/d0a8189e7a490011162d85db97d6efcd +++ /dev/null @@ -1,5 +0,0 @@ -package comparatori.videogioco; - -public class Main { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/81/a0de638e7f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/81/a0de638e7f490011162d85db97d6efcd deleted file mode 100644 index cd961fc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/81/a0de638e7f490011162d85db97d6efcd +++ /dev/null @@ -1,27 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/81/f0a7db2863490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/81/f0a7db2863490011162d85db97d6efcd deleted file mode 100644 index 00129cf..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/81/f0a7db2863490011162d85db97d6efcd +++ /dev/null @@ -1,516 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - makeFull(); - - BinaryNode copy; - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - copy = new BinaryNode(node.getRight().getData()); - node.setLeft(copy); - copy.setParentAsLeftChild(node); - } else { - copy = new BinaryNode(node.getLeft().getData()); - node.setRight(copy); - copy.setParentAsRightChild(node); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/102226a57f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/83/102226a57f490011162d85db97d6efcd deleted file mode 100644 index 2104af7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/83/102226a57f490011162d85db97d6efcd +++ /dev/null @@ -1,30 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!set.add(x)) { - - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/509aac9078490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/83/509aac9078490011162d85db97d6efcd deleted file mode 100644 index ef0c3d2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/83/509aac9078490011162d85db97d6efcd +++ /dev/null @@ -1,21 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco3 implements Comparator { - - /** - * Prima ordina per piattaforma alfabetica. - * Se la piattaforma è uguale, per anno - * di uscita crescente - * se l'anno è uguale, allora per titolo alfabetico - */ - @Override - public int compare(Videogioco v1, Videogioco v2) { - int cmp = v1.piattaforma().compareTo(v2.piattaforma()); - if (cmp != 0) return cmp; - cmp = v1.annoUscita() - v2.annoUscita(); - if (cmp != 0) return cmp; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/b0282bd675490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/83/b0282bd675490011162d85db97d6efcd deleted file mode 100644 index bebc5a0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/83/b0282bd675490011162d85db97d6efcd +++ /dev/null @@ -1,25 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/84/70e3fa6f5f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/84/70e3fa6f5f490011162d85db97d6efcd deleted file mode 100644 index 77ea04d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/84/70e3fa6f5f490011162d85db97d6efcd +++ /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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/84/d01430a67b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/84/d01430a67b490011162d85db97d6efcd deleted file mode 100644 index 4bdb994..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/84/d01430a67b490011162d85db97d6efcd +++ /dev/null @@ -1,35 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - Collections.sort(giochi); - - - - } - - private void printList(List lista) { - for (int i = 0; i < lista.size(); i++) { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/85/6055b2e57f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/85/6055b2e57f490011162d85db97d6efcd deleted file mode 100644 index 299722c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/85/6055b2e57f490011162d85db97d6efcd +++ /dev/null @@ -1,30 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } else System.out.println("Intero inserito: " + x); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/85/b07fb9694d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/85/b07fb9694d490011162d85db97d6efcd deleted file mode 100644 index 5bfdaf1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/85/b07fb9694d490011162d85db97d6efcd +++ /dev/null @@ -1,51 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - public static Integer maxValue(BinaryTree btree) { - Iterator iterator = btree.iteratorInOrder(); - if (!iterator.hasNext()) return null; - Integer maxValue = iterator.next(); - while () { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/86/50bf7b7e4b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/86/50bf7b7e4b490011162d85db97d6efcd deleted file mode 100644 index 870d842..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/86/50bf7b7e4b490011162d85db97d6efcd +++ /dev/null @@ -1,24 +0,0 @@ -package binary_tree; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iterator(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/86/7012d43578490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/86/7012d43578490011162d85db97d6efcd deleted file mode 100644 index af72712..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/86/7012d43578490011162d85db97d6efcd +++ /dev/null @@ -1,18 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco3 implements Comparator { - - /** - * Prima ordina per piattaforma alfabetica. - * Se la piattaforma è uguale, per anno - * di uscita crescente - * se l'anno è uguale, allora per titolo alfabetico - */ - @Override - public int compare(Videogioco v1, Videogioco v2) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/87/908ea3bd75490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/87/908ea3bd75490011162d85db97d6efcd deleted file mode 100644 index af06c47..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/87/908ea3bd75490011162d85db97d6efcd +++ /dev/null @@ -1,24 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/88/40313a667f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/88/40313a667f490011162d85db97d6efcd deleted file mode 100644 index c8443bc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/88/40313a667f490011162d85db97d6efcd +++ /dev/null @@ -1,22 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/88/906b9d3b80490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/88/906b9d3b80490011162d85db97d6efcd deleted file mode 100644 index 572bd88..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/88/906b9d3b80490011162d85db97d6efcd +++ /dev/null @@ -1,37 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - new InteriRipetuti().run(); - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET (non ordinato) - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } else System.out.println("Intero inserito: " + x); - } - - // Stampa di hashset - System.out.println("Hashset"); - for (Integer x : hashset) System.out.println(x + " "); - System.out.println(); System.out.println(hashset); - - // TREESET (ordinato) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/89/80797a724b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/89/80797a724b490011162d85db97d6efcd deleted file mode 100644 index 1abfd4e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/89/80797a724b490011162d85db97d6efcd +++ /dev/null @@ -1,24 +0,0 @@ -package binary_tree; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8a/20df8efa4e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8a/20df8efa4e490011162d85db97d6efcd deleted file mode 100644 index b0a9a13..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8a/20df8efa4e490011162d85db97d6efcd +++ /dev/null @@ -1,61 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - public static Integer maxValue(BinaryTree btree) { - Iterator iterator = btree.iteratorInOrder(); - if (!iterator.hasNext()) return null; - Integer maxValue = iterator.next(); - while (iterator.hasNext()) { - Integer current = iterator.next(); - if (current > maxValue) maxValue = current; - } - return maxValue; - } - - /* - * 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/8a/50cd25024c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8a/50cd25024c490011162d85db97d6efcd deleted file mode 100644 index 122d4de..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8a/50cd25024c490011162d85db97d6efcd +++ /dev/null @@ -1,30 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8a/706925f277490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8a/706925f277490011162d85db97d6efcd deleted file mode 100644 index e69de29..0000000 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8b/8036a58375490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/8036a58375490011162d85db97d6efcd deleted file mode 100644 index 1910fbd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8b/8036a58375490011162d85db97d6efcd +++ /dev/null @@ -1,8 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco { - - // Campi di istanza - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8b/902b548560490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/902b548560490011162d85db97d6efcd deleted file mode 100644 index a4b991d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8b/902b548560490011162d85db97d6efcd +++ /dev/null @@ -1,504 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia - - if () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8b/d08ae6af7c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/d08ae6af7c490011162d85db97d6efcd deleted file mode 100644 index 006cd6f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8b/d08ae6af7c490011162d85db97d6efcd +++ /dev/null @@ -1,41 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - Collections.sort(giochi); - printList(giochi); - - // Ordina i videogiochi per titolo alfabetico crescente - - - } - - private static void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8b/f0077d9c7f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/f0077d9c7f490011162d85db97d6efcd deleted file mode 100644 index ad52513..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8b/f0077d9c7f490011162d85db97d6efcd +++ /dev/null @@ -1,28 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8c/30c326fe5f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/30c326fe5f490011162d85db97d6efcd deleted file mode 100644 index df67d42..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8c/30c326fe5f490011162d85db97d6efcd +++ /dev/null @@ -1,500 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/e08c3c447b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/e08c3c447b490011162d85db97d6efcd deleted file mode 100644 index d2fdb70..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/e08c3c447b490011162d85db97d6efcd +++ /dev/null @@ -1,32 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - Collections.sort(giochi); - - - - } - - private T void printList(List lista) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8f/30b7a4e87b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8f/30b7a4e87b490011162d85db97d6efcd deleted file mode 100644 index dff7e76..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8f/30b7a4e87b490011162d85db97d6efcd +++ /dev/null @@ -1,66 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } - - public Integer annoUscita() { - return annoUscita; - } - - public Double prezzo() { - return prezzo; - } - - public Integer voto() { - return voto; - } - - // Implementazione del metodo comparatore - - /** - * compareTo ordina per prezzo crescente - */ - @Override - public int compareTo(Videogioco videogioco) { - if (this.prezzo < videogioco.prezzo) return -1; - if (this.prezzo > videogioco.prezzo) return 1; - return 0; - } - - @Override - public String toString() { - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9/402bd10e4b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9/402bd10e4b490011162d85db97d6efcd deleted file mode 100644 index 009f378..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/9/402bd10e4b490011162d85db97d6efcd +++ /dev/null @@ -1,17 +0,0 @@ -package binary_tree; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - public static boolean isEvenBtree(BinaryTree btree) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9/e08523e55e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9/e08523e55e490011162d85db97d6efcd deleted file mode 100644 index 6537153..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/9/e08523e55e490011162d85db97d6efcd +++ /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; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - int i; - - - return n - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/91/1052254876490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/91/1052254876490011162d85db97d6efcd deleted file mode 100644 index e2b2777..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/91/1052254876490011162d85db97d6efcd +++ /dev/null @@ -1,51 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } - - public Integer annoUscita() { - return annoUscita; - } - - public Double prezzo() { - return prezzo; - } - - public Integer voto() { - return voto; - } - - // Implementazione del metodo comparatore -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/91/603a0fd07b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/91/603a0fd07b490011162d85db97d6efcd deleted file mode 100644 index 19087d2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/91/603a0fd07b490011162d85db97d6efcd +++ /dev/null @@ -1,39 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - Collections.sort(giochi); - - - - } - - private void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(""); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/92/007aafd960490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/92/007aafd960490011162d85db97d6efcd deleted file mode 100644 index 6359ed8..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/92/007aafd960490011162d85db97d6efcd +++ /dev/null @@ -1,508 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - if (!node.hasLeft()) { - - } else { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/92/309fa0967b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/92/309fa0967b490011162d85db97d6efcd deleted file mode 100644 index 2fbb42c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/92/309fa0967b490011162d85db97d6efcd +++ /dev/null @@ -1,33 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - Collections.sort(giochi); - - - - } - - private void printList(List lista) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/95/c00ab97d7b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/95/c00ab97d7b490011162d85db97d6efcd deleted file mode 100644 index 872a265..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/95/c00ab97d7b490011162d85db97d6efcd +++ /dev/null @@ -1,33 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - Collections.sort(giochi); - - - - } - - private T void printList(List lista) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/004de8e976490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/97/004de8e976490011162d85db97d6efcd deleted file mode 100644 index 3b5bb3e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/97/004de8e976490011162d85db97d6efcd +++ /dev/null @@ -1,5 +0,0 @@ -package comparatori.videogioco; - -public class ComparatorVideogioco1 { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/d0c14f7b4d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/97/d0c14f7b4d490011162d85db97d6efcd deleted file mode 100644 index 3c3e5c5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/97/d0c14f7b4d490011162d85db97d6efcd +++ /dev/null @@ -1,51 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - public static Integer maxValue(BinaryTree btree) { - Iterator iterator = btree.iteratorInOrder(); - if (!iterator.hasNext()) return null; - Integer maxValue = iterator.next(); - while (iterator.hasNext()) { - Integer current; - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/98/40f6441b7d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/98/40f6441b7d490011162d85db97d6efcd deleted file mode 100644 index eece3e5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/98/40f6441b7d490011162d85db97d6efcd +++ /dev/null @@ -1,54 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - System.out.println("ordina per prezzo crescente"); - Collections.sort(giochi); - printList(giochi); - - // Ordina i videogiochi per titolo alfabetico crescente - System.out.println("Ordina i videogiochi per titolo alfabetico crescente"); - Collections.sort(giochi, new ComparatorVideogioco1()); - printList(giochi); - - // Ordina dal voto più alto al più basso - System.out.println("Ordina dal voto più alto al più basso"); - Collections.sort(giochi, new ComparatorVideogioco2()); - printList(giochi); - - // Multi - System.out.println("Multi"); - Collections.sort(giochi, new ComparatorVideogioco2()); - printList(giochi); - - } - - private static void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/99/50a39d0276490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/99/50a39d0276490011162d85db97d6efcd deleted file mode 100644 index 2ab2156..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/99/50a39d0276490011162d85db97d6efcd +++ /dev/null @@ -1,31 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9a/201388db77490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/201388db77490011162d85db97d6efcd deleted file mode 100644 index 28e65df..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/9a/201388db77490011162d85db97d6efcd +++ /dev/null @@ -1,16 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco2 implements Comparator { - - /** - * Ordina dal voto più alto al più basso - * @return - */ - @Override - public int compare() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9b/109551477b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9b/109551477b490011162d85db97d6efcd deleted file mode 100644 index fdb2a7f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/9b/109551477b490011162d85db97d6efcd +++ /dev/null @@ -1,33 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - Collections.sort(giochi); - - - - } - - private void T printList(List lista) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9e/106bfbff7e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9e/106bfbff7e490011162d85db97d6efcd deleted file mode 100644 index 1a4e161..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/9e/106bfbff7e490011162d85db97d6efcd +++ /dev/null @@ -1,5 +0,0 @@ -package jcf_set.example; - -public class InteriRipetuti { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/0005d07276490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/0005d07276490011162d85db97d6efcd deleted file mode 100644 index a301c8c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/0005d07276490011162d85db97d6efcd +++ /dev/null @@ -1,61 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } - - public Integer annoUscita() { - return annoUscita; - } - - public Double prezzo() { - return prezzo; - } - - public Integer voto() { - return voto; - } - - // Implementazione del metodo comparatore - - /** - * compareTo ordina per prezzo crescente - */ - @Override - public int compareTo(Videogioco videogioco) { - - if (this.prezzo < videogioco.prezzo) return -1; - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/f0cafd0f78490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/f0cafd0f78490011162d85db97d6efcd deleted file mode 100644 index 9179555..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/f0cafd0f78490011162d85db97d6efcd +++ /dev/null @@ -1,12 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco3 implements Comparator { - - @Override - public int compare(Videogioco v1, Videogioco v2) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/9056884262490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/9056884262490011162d85db97d6efcd deleted file mode 100644 index f0ab9d5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/9056884262490011162d85db97d6efcd +++ /dev/null @@ -1,511 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - BinaryNode copy; - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - copy = new BinaryNode(); - } else { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a5/70f40a5180490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a5/70f40a5180490011162d85db97d6efcd deleted file mode 100644 index 3810cd0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a5/70f40a5180490011162d85db97d6efcd +++ /dev/null @@ -1,41 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; -import java.util.TreeSet; - -public class InteriRipetuti { - - public static void main(String[] Args) { - new InteriRipetuti().run(); - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET (non ordinato) - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } else System.out.println("Intero inserito: " + x); - } - - // Stampa di hashset - System.out.println("Hashset"); - for (Integer x : hashset) System.out.println(x + " "); - System.out.println(); System.out.println(hashset); - - // TREESET (ordinato) - Set treeset = new TreeSet(hashset); - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a6/205543404d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/205543404d490011162d85db97d6efcd deleted file mode 100644 index 764dd69..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a6/205543404d490011162d85db97d6efcd +++ /dev/null @@ -1,48 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - public static Integer maxValue(BinaryTree btree) { - - Integer maxValue; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a7/70133e1277490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a7/70133e1277490011162d85db97d6efcd deleted file mode 100644 index 0072c98..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a7/70133e1277490011162d85db97d6efcd +++ /dev/null @@ -1,5 +0,0 @@ -package comparatori.videogioco; - -public class ComparatorVideogioco1 implements Comparator { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/a0238d6080490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/a0238d6080490011162d85db97d6efcd deleted file mode 100644 index 0f703d9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/a0238d6080490011162d85db97d6efcd +++ /dev/null @@ -1,44 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; -import java.util.TreeSet; - -public class InteriRipetuti { - - public static void main(String[] Args) { - new InteriRipetuti().run(); - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET (non ordinato) - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } else System.out.println("Intero inserito: " + x); - } - - // Stampa di hashset - System.out.println("Hashset"); - for (Integer x : hashset) System.out.println(x + " "); - System.out.println(); System.out.println(hashset); - - // TREESET (ordinato) - Set treeset = new TreeSet(hashset); - - // Stampa di treeset - System.out.println("Treeset"); - for (Integer x : hashset) System.out.println(x + " "); - System.out.println(); System.out.println(hashset); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ab/60ee3bc64a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ab/60ee3bc64a490011162d85db97d6efcd deleted file mode 100644 index e69de29..0000000 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/c0b8e39f62490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/c0b8e39f62490011162d85db97d6efcd deleted file mode 100644 index d3c1ca3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/c0b8e39f62490011162d85db97d6efcd +++ /dev/null @@ -1,513 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - BinaryNode copy; - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - copy = new BinaryNode(node.getRight().getData()); - node.setLeft(copy); - copy.setParentAsLeftChild(node); - } else { - copy = new BinaryNode(node.getLeft().getData()); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ae/3062d10a7d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ae/3062d10a7d490011162d85db97d6efcd deleted file mode 100644 index 69ace53..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ae/3062d10a7d490011162d85db97d6efcd +++ /dev/null @@ -1,44 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - System.out.println("ordina per prezzo crescente"); - Collections.sort(giochi); - printList(giochi); - - // Ordina i videogiochi per titolo alfabetico crescente - System.out.println("Ordina i videogiochi per titolo alfabetico crescente"); - Collections.sort(giochi, new ComparatorVideogioco1()); - printList(giochi); - - } - - private static void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b/4057991677490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b/4057991677490011162d85db97d6efcd deleted file mode 100644 index 2420342..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b/4057991677490011162d85db97d6efcd +++ /dev/null @@ -1,13 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco1 implements Comparator { - - /** - * Ordina i videogiochi per titolo alfabetico crescente - */ - @Override - public - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b0/60ab100b4c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b0/60ab100b4c490011162d85db97d6efcd deleted file mode 100644 index d7f9b1b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b0/60ab100b4c490011162d85db97d6efcd +++ /dev/null @@ -1,30 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if (current % 2 == 0) - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/c09bfc7c7f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/c09bfc7c7f490011162d85db97d6efcd deleted file mode 100644 index 0626b36..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/c09bfc7c7f490011162d85db97d6efcd +++ /dev/null @@ -1,25 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - for (int i = 0; i < N; i++) { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b2/e02b9c497c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b2/e02b9c497c490011162d85db97d6efcd deleted file mode 100644 index 0236371..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b2/e02b9c497c490011162d85db97d6efcd +++ /dev/null @@ -1,39 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - Collections.sort(giochi); - printList(giochi); - - - } - - private static void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b4/10e34d1f77490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b4/10e34d1f77490011162d85db97d6efcd deleted file mode 100644 index 2d9f19c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b4/10e34d1f77490011162d85db97d6efcd +++ /dev/null @@ -1,15 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco1 implements Comparator { - - /** - * Ordina i videogiochi per titolo alfabetico crescente - */ - @Override - public int compareTo() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b5/c006797e78490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b5/c006797e78490011162d85db97d6efcd deleted file mode 100644 index 9325297..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b5/c006797e78490011162d85db97d6efcd +++ /dev/null @@ -1,20 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco3 implements Comparator { - - /** - * Prima ordina per piattaforma alfabetica. - * Se la piattaforma è uguale, per anno - * di uscita crescente - * se l'anno è uguale, allora per titolo alfabetico - */ - @Override - public int compare(Videogioco v1, Videogioco v2) { - int cmp = v1.piattaforma().compareTo(v2.piattaforma()); - if (cmp == 0) return cmp; - cmp = v1.annoUscita() - v2.annoUscita(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/108d87427f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/108d87427f490011162d85db97d6efcd deleted file mode 100644 index 10fb185..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/108d87427f490011162d85db97d6efcd +++ /dev/null @@ -1,17 +0,0 @@ -package jcf_set.example; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set set = new HashSet(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/e04f37e74c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/e04f37e74c490011162d85db97d6efcd deleted file mode 100644 index 9413b66..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/e04f37e74c490011162d85db97d6efcd +++ /dev/null @@ -1,35 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b8/40bd229f63490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/40bd229f63490011162d85db97d6efcd deleted file mode 100644 index 3de8154..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b8/40bd229f63490011162d85db97d6efcd +++ /dev/null @@ -1,517 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - makeFull(node.getLeft()); - makeFull(node.getRight()); - - BinaryNode copy; - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - copy = new BinaryNode(node.getRight().getData()); - node.setLeft(copy); - copy.setParentAsLeftChild(node); - } else { - copy = new BinaryNode(node.getLeft().getData()); - node.setRight(copy); - copy.setParentAsRightChild(node); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b8/b004ba1580490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/b004ba1580490011162d85db97d6efcd deleted file mode 100644 index 4cdb06e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b8/b004ba1580490011162d85db97d6efcd +++ /dev/null @@ -1,35 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } else System.out.println("Intero inserito: " + x); - } - - // Stampa di hashset - System.out.println("Hashset"); - for (Integer x : hashset) System.out.println(x + " "); - System.out.println(); System.out.println(set); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b9/0031abfa5f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b9/0031abfa5f490011162d85db97d6efcd deleted file mode 100644 index 7ba57b6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b9/0031abfa5f490011162d85db97d6efcd +++ /dev/null @@ -1,500 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ba/503dcaeb7f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/503dcaeb7f490011162d85db97d6efcd deleted file mode 100644 index b81e724..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ba/503dcaeb7f490011162d85db97d6efcd +++ /dev/null @@ -1,33 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } else System.out.println("Intero inserito: " + x); - } - - // Stampa di hashset - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/0073a49377490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/0073a49377490011162d85db97d6efcd deleted file mode 100644 index e69de29..0000000 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/d063a1ea76490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/d063a1ea76490011162d85db97d6efcd deleted file mode 100644 index 6495c29..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/d063a1ea76490011162d85db97d6efcd +++ /dev/null @@ -1,5 +0,0 @@ -package comparatori.videogioco; - -public class ComparatorVideogioco1 implements Comparator{ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/e0a49df05f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/e0a49df05f490011162d85db97d6efcd deleted file mode 100644 index 1092ac3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/e0a49df05f490011162d85db97d6efcd +++ /dev/null @@ -1,500 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode implements BinaryTree{ - - // VARIABILI D'INSTANZA - private BinaryNode root; - private int size; - - // Metodi costruttori - public LinkedBinaryTree() { - root = null; - size = 0; - } - - public LinkedBinaryTree(E data) { - root = new BinaryNode(data); - size = 1; - } - - public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { - root = new BinaryNode(left.root, data, right.root); - size = 1 + left.size + right.size; - } - - // METODI - - public int size() { - return size; - } - - @Override - public boolean isEmpty() { - return root == null; - } - - public E getRoot() { - if (isEmpty()) return null; - return root.getData(); - } - - public BinaryNode getRootNode() { - if (isEmpty()) return null; - return root; - } - - public void clear() { - root = null; - size = 0; - } - - private int getSize(BinaryNode node) { - if (node == null) return 0; - - int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); - - return 1 + nLeft + nRight; - } - - public LinkedBinaryTree removeLeft() { - LinkedBinaryTree leftTree = null; - if (root.getLeft() == null) return leftTree; - - leftTree = new LinkedBinaryTree(); - leftTree.root = root.getLeft(); - leftTree.size = getSize(root.getLeft()); - leftTree.root.setAsRoot(); - - size = size - leftTree.size; - - return leftTree; - } - - public LinkedBinaryTree removeRight() { - LinkedBinaryTree rightTree = null; - if (root.getRight() == null) return rightTree; - - rightTree = new LinkedBinaryTree(); - rightTree.root = root.getRight(); - rightTree.size = getSize(root.getRight()); - rightTree.root.setAsRoot(); - - size = size - rightTree.size; - - return rightTree; - } - - protected BinaryNode find(E targetElement, BinaryNode root) { - if (root == null) return null; - if (root.getData().equals(targetElement)) return root; - BinaryNode resNode; - resNode = find(targetElement, root.getLeft()); - if (resNode == null) resNode = find(targetElement, root.getRight()); - return resNode; - } - - public boolean remove(E targetElement) { - if (targetElement == null) return false; - BinaryNode temp = find(targetElement, root); - if (temp != null) { - temp.setData(null); - return true; - } - return false; - } - - public boolean contains(E targetElement) { - return find(targetElement, root) != null; - } - - // ITERATORE PREORDER - - /* - * Qui si utilizza il metodo del preorder. - * Le regole per il preorder sono queste: - * - Visita prima se stesso - * - Poi visita tutto ciò che si trova sinistra - * - Infine visita tutto ciò che si trova a destra - */ - - protected void preorder (BinaryNode node, List temporaryList) { - if (node == null) return; - - if (node.getData() != null) temporaryList.add(node.getData()); - - preorder(node.getLeft(), temporaryList); - preorder(node.getRight(), temporaryList); - } - - public Iterator iteratorPreOrder() { - ArrayList temporaryList = new ArrayList(); - preorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE INORDER - - /* - * Qui si utilizza il metodo del inorder. - * Le regole per l'inorder sono queste: - * - Visita prima tutto ciò che si trova a sinistra - * - Poi visita se stesso - * - Infine visita tutto ciò che si trova a destra - */ - - protected void inorder(BinaryNode node, List temporaryList) { - if (node == null) return; - - inorder(node.getLeft(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - - inorder(node.getRight(), temporaryList); - } - - public Iterator iteratorInOrder() { - ArrayList temporaryList = new ArrayList(); - inorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE POSTORDER - - /* - * Qui si utilizza il metodo postorder. - * Le regole per il postorder sono queste: - * - Visita prima di tutto ciò che si trova a sinistra - * - Poi visita tutto ciò che si trova a destra - * - Infine visita il sestesso - */ - - protected void postorder(BinaryNode node, List temporaryList) { - if (node == null) return; - - postorder(node.getLeft(), temporaryList); - postorder(node.getRight(), temporaryList); - - if (node.getData() != null) temporaryList.add(node.getData()); - } - - public Iterator iteratorPostOrder() { - ArrayList temporaryList = new ArrayList(); - postorder(root, temporaryList); - return temporaryList.iterator(); - } - - // ITERATORE LEVEL ORDER - - /* - * Qui si usa il metodo levelorder. - * Si analizza tutto il livello dell'albero. - * Per chiarire di seguito un esempio: - * - * A - * / \ - * B C - * / \ \ - * D E F - * - * La lettura avviene quindi in questo modo: - * A - B - C - D - E - F - * - * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) - */ - - protected void levelorder(BinaryNode node, List temporaryList) { - Queue> queueOfNodes = new LinkedList>(); - BinaryNode current; - - queueOfNodes.add(node); - while(!queueOfNodes.isEmpty()) { - current = queueOfNodes.remove(); - - if (current.getData() != null) temporaryList.add(current.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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - } else { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/00578b275f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/00578b275f490011162d85db97d6efcd deleted file mode 100644 index dd18429..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/00578b275f490011162d85db97d6efcd +++ /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); - return number; - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/be/9067867476490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/be/9067867476490011162d85db97d6efcd deleted file mode 100644 index ef39891..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/be/9067867476490011162d85db97d6efcd +++ /dev/null @@ -1,62 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } - - public Integer annoUscita() { - return annoUscita; - } - - public Double prezzo() { - return prezzo; - } - - public Integer voto() { - return voto; - } - - // Implementazione del metodo comparatore - - /** - * compareTo ordina per prezzo crescente - */ - @Override - public int compareTo(Videogioco videogioco) { - - if (this.prezzo < videogioco.prezzo) return -1; - if (this.prezzo > videogioco.prezzo) return 1; - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/e040508c7f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/e040508c7f490011162d85db97d6efcd deleted file mode 100644 index 575fd54..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/e040508c7f490011162d85db97d6efcd +++ /dev/null @@ -1,25 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/f0b022e65f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/f0b022e65f490011162d85db97d6efcd deleted file mode 100644 index 832f2fd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/f0b022e65f490011162d85db97d6efcd +++ /dev/null @@ -1,500 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - - } - - public void makeFull() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c/507a02e25f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c/507a02e25f490011162d85db97d6efcd deleted file mode 100644 index abb0216..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c/507a02e25f490011162d85db97d6efcd +++ /dev/null @@ -1,496 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/90e1ca0b76490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/90e1ca0b76490011162d85db97d6efcd deleted file mode 100644 index f291081..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/90e1ca0b76490011162d85db97d6efcd +++ /dev/null @@ -1,37 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c4/103da84d62490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c4/103da84d62490011162d85db97d6efcd deleted file mode 100644 index 17761ef..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c4/103da84d62490011162d85db97d6efcd +++ /dev/null @@ -1,511 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - BinaryNode copy; - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - copy = new BinaryNode(node.getRight().getData()); - } else { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c4/c0caa0cc7f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c4/c0caa0cc7f490011162d85db97d6efcd deleted file mode 100644 index 51014e3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c4/c0caa0cc7f490011162d85db97d6efcd +++ /dev/null @@ -1,30 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto"); - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/b0a3ec2576490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/b0a3ec2576490011162d85db97d6efcd deleted file mode 100644 index f947e25..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/b0a3ec2576490011162d85db97d6efcd +++ /dev/null @@ -1,45 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } - - public Integer annoUscita() { - return annoUscita; - } - - public Double prezzo() { - return prezzo; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/b0d47f644b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/b0d47f644b490011162d85db97d6efcd deleted file mode 100644 index 57b218d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/b0d47f644b490011162d85db97d6efcd +++ /dev/null @@ -1,24 +0,0 @@ -package binary_tree; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/d0877e594d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/d0877e594d490011162d85db97d6efcd deleted file mode 100644 index dcb628c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/d0877e594d490011162d85db97d6efcd +++ /dev/null @@ -1,49 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - public static Integer maxValue(BinaryTree btree) { - Iterator iterator = btree.iteratorInOrder(); - if (!iterator.hasNext()) return null; - Integer maxValue = iterator.next(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c8/40f828dd7f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/40f828dd7f490011162d85db97d6efcd deleted file mode 100644 index 8d3da70..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c8/40f828dd7f490011162d85db97d6efcd +++ /dev/null @@ -1,30 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c9/d0b245d160490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/d0b245d160490011162d85db97d6efcd deleted file mode 100644 index f910670..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c9/d0b245d160490011162d85db97d6efcd +++ /dev/null @@ -1,504 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - if (!node.hasLeft()) - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cb/c08a279a75490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cb/c08a279a75490011162d85db97d6efcd deleted file mode 100644 index b90beb9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cb/c08a279a75490011162d85db97d6efcd +++ /dev/null @@ -1,17 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco {} - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/10f9397d62490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/10f9397d62490011162d85db97d6efcd deleted file mode 100644 index 424f445..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/10f9397d62490011162d85db97d6efcd +++ /dev/null @@ -1,511 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - BinaryNode copy; - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - copy = new BinaryNode(node.getRight().getData()); - } else { - copy = new BinaryNode(node.getLeft().getData()); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/2063cc0578490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/2063cc0578490011162d85db97d6efcd deleted file mode 100644 index 6798eef..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/2063cc0578490011162d85db97d6efcd +++ /dev/null @@ -1,5 +0,0 @@ -package comparatori.videogioco; - -public class ComparatorVideogioco3 implements Comparator { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/80cdc9637f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/80cdc9637f490011162d85db97d6efcd deleted file mode 100644 index 56495be..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/80cdc9637f490011162d85db97d6efcd +++ /dev/null @@ -1,21 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/d0a9170e4c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/d0a9170e4c490011162d85db97d6efcd deleted file mode 100644 index e810abb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/d0a9170e4c490011162d85db97d6efcd +++ /dev/null @@ -1,30 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if (current % 2 == 0) even = false; - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d/a072985e4b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d/a072985e4b490011162d85db97d6efcd deleted file mode 100644 index 57b218d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d/a072985e4b490011162d85db97d6efcd +++ /dev/null @@ -1,24 +0,0 @@ -package binary_tree; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d/c0a00c864d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d/c0a00c864d490011162d85db97d6efcd deleted file mode 100644 index 219ed21..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d/c0a00c864d490011162d85db97d6efcd +++ /dev/null @@ -1,52 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - public static Integer maxValue(BinaryTree btree) { - Iterator iterator = btree.iteratorInOrder(); - if (!iterator.hasNext()) return null; - Integer maxValue = iterator.next(); - while (iterator.hasNext()) { - Integer current = iterator.next(); - if () - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d0/803500214c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/803500214c490011162d85db97d6efcd deleted file mode 100644 index a31c38c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d0/803500214c490011162d85db97d6efcd +++ /dev/null @@ -1,31 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if (current % 2 == 0) even = false; - } - return even; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/80b24cb377490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/80b24cb377490011162d85db97d6efcd deleted file mode 100644 index 0995790..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/80b24cb377490011162d85db97d6efcd +++ /dev/null @@ -1,13 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco2 implements Comparator { - - - @Override - public int compare() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/c0cbf25378490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/c0cbf25378490011162d85db97d6efcd deleted file mode 100644 index 5432b30..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/c0cbf25378490011162d85db97d6efcd +++ /dev/null @@ -1,18 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco3 implements Comparator { - - /** - * Prima ordina per piattaforma alfabetica. - * Se la piattaforma è uguale, per anno - * di uscita crescente - * se l'anno è uguale, allora per titolo alfabetico - */ - @Override - public int compare(Videogioco v1, Videogioco v2) { - int cpm = v1.piattaforma().compareTo(v2.piattaforma()); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d3/e092bfd74a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d3/e092bfd74a490011162d85db97d6efcd deleted file mode 100644 index 980b62c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d3/e092bfd74a490011162d85db97d6efcd +++ /dev/null @@ -1,5 +0,0 @@ -package binary_tree; - -public class ExerciseBinaryTree { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d4/00a4846175490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d4/00a4846175490011162d85db97d6efcd deleted file mode 100644 index 12847ab..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d4/00a4846175490011162d85db97d6efcd +++ /dev/null @@ -1,5 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d6/0019fb894d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d6/0019fb894d490011162d85db97d6efcd deleted file mode 100644 index fa7440e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d6/0019fb894d490011162d85db97d6efcd +++ /dev/null @@ -1,52 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - public static Integer maxValue(BinaryTree btree) { - Iterator iterator = btree.iteratorInOrder(); - if (!iterator.hasNext()) return null; - Integer maxValue = iterator.next(); - while (iterator.hasNext()) { - Integer current = iterator.next(); - if (current > maxValue) maxValue = current; - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d7/306e90e27a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d7/306e90e27a490011162d85db97d6efcd deleted file mode 100644 index adc729a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d7/306e90e27a490011162d85db97d6efcd +++ /dev/null @@ -1,17 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - - - // Per adoperare il comparable - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d9/002585297c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/002585297c490011162d85db97d6efcd deleted file mode 100644 index 50e6917..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d9/002585297c490011162d85db97d6efcd +++ /dev/null @@ -1,39 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - Collections.sort(giochi); - printList(); - - - } - - private void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d9/90bfa8e175490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/90bfa8e175490011162d85db97d6efcd deleted file mode 100644 index 2ee1db4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d9/90bfa8e175490011162d85db97d6efcd +++ /dev/null @@ -1,26 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d9/c0c28e6e7f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/c0c28e6e7f490011162d85db97d6efcd deleted file mode 100644 index 8cddde5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d9/c0c28e6e7f490011162d85db97d6efcd +++ /dev/null @@ -1,25 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - for () { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/da/50a010315f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/da/50a010315f490011162d85db97d6efcd deleted file mode 100644 index 24c48ad..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/da/50a010315f490011162d85db97d6efcd +++ /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; - number = numberLeaf(root); - return number; - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/da/9033323278490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/da/9033323278490011162d85db97d6efcd deleted file mode 100644 index 7c5decc..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/da/9033323278490011162d85db97d6efcd +++ /dev/null @@ -1,15 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco3 implements Comparator { - - /** - * - */ - @Override - public int compare(Videogioco v1, Videogioco v2) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/da/e036aa2f7d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/da/e036aa2f7d490011162d85db97d6efcd deleted file mode 100644 index 6bad699..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/da/e036aa2f7d490011162d85db97d6efcd +++ /dev/null @@ -1,55 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - System.out.println("ordina per prezzo crescente"); - Collections.sort(giochi); - printList(giochi); - System.out.println(); - - // Ordina i videogiochi per titolo alfabetico crescente - System.out.println("Ordina i videogiochi per titolo alfabetico crescente"); - Collections.sort(giochi, new ComparatorVideogioco1()); - printList(giochi); - - // Ordina dal voto più alto al più basso - System.out.println("Ordina dal voto più alto al più basso"); - Collections.sort(giochi, new ComparatorVideogioco2()); - printList(giochi); - - // Multi - System.out.println("Multi"); - Collections.sort(giochi, new ComparatorVideogioco3()); - printList(giochi); - - } - - private static void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/db/b0e0579275490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/db/b0e0579275490011162d85db97d6efcd deleted file mode 100644 index 245cdee..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/db/b0e0579275490011162d85db97d6efcd +++ /dev/null @@ -1,12 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/30d8b4457f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/30d8b4457f490011162d85db97d6efcd deleted file mode 100644 index b4bfecf..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/30d8b4457f490011162d85db97d6efcd +++ /dev/null @@ -1,20 +0,0 @@ -package jcf_set.example; - -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/503bd80960490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/503bd80960490011162d85db97d6efcd deleted file mode 100644 index a6be25b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/503bd80960490011162d85db97d6efcd +++ /dev/null @@ -1,501 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/f09e80bc60490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/f09e80bc60490011162d85db97d6efcd deleted file mode 100644 index 90c01bd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/f09e80bc60490011162d85db97d6efcd +++ /dev/null @@ -1,504 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/de/50724e1476490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/de/50724e1476490011162d85db97d6efcd deleted file mode 100644 index 0dd1807..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/de/50724e1476490011162d85db97d6efcd +++ /dev/null @@ -1,37 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/de/606c53175f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/de/606c53175f490011162d85db97d6efcd deleted file mode 100644 index ff63d54..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/de/606c53175f490011162d85db97d6efcd +++ /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 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e/90d154294d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e/90d154294d490011162d85db97d6efcd deleted file mode 100644 index 1a85fb7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e/90d154294d490011162d85db97d6efcd +++ /dev/null @@ -1,46 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - public static Integer maxValue(BinaryTree btree) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e1/50f446624e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e1/50f446624e490011162d85db97d6efcd deleted file mode 100644 index f22aaef..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e1/50f446624e490011162d85db97d6efcd +++ /dev/null @@ -1,55 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - public static Integer maxValue(BinaryTree btree) { - Iterator iterator = btree.iteratorInOrder(); - if (!iterator.hasNext()) return null; - Integer maxValue = iterator.next(); - while (iterator.hasNext()) { - Integer current = iterator.next(); - if (current > maxValue) maxValue = current; - } - return maxValue; - } - - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e2/80c9d09775490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/80c9d09775490011162d85db97d6efcd deleted file mode 100644 index 9b84665..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e2/80c9d09775490011162d85db97d6efcd +++ /dev/null @@ -1,13 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e6/c06eabe84b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e6/c06eabe84b490011162d85db97d6efcd deleted file mode 100644 index 1fd5fe0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e6/c06eabe84b490011162d85db97d6efcd +++ /dev/null @@ -1,27 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e7/808a52ce60490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/808a52ce60490011162d85db97d6efcd deleted file mode 100644 index 3c4cd92..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e7/808a52ce60490011162d85db97d6efcd +++ /dev/null @@ -1,504 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - if () - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e8/7048924d80490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e8/7048924d80490011162d85db97d6efcd deleted file mode 100644 index cc741fd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e8/7048924d80490011162d85db97d6efcd +++ /dev/null @@ -1,38 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - new InteriRipetuti().run(); - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET (non ordinato) - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } else System.out.println("Intero inserito: " + x); - } - - // Stampa di hashset - System.out.println("Hashset"); - for (Integer x : hashset) System.out.println(x + " "); - System.out.println(); System.out.println(hashset); - - // TREESET (ordinato) - Set treeset = new TreeSet(hashset); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e8/e0f0342976490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e8/e0f0342976490011162d85db97d6efcd deleted file mode 100644 index 0b249b1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e8/e0f0342976490011162d85db97d6efcd +++ /dev/null @@ -1,49 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } - - public Integer annoUscita() { - return annoUscita; - } - - public Double prezzo() { - return prezzo; - } - - public Integer voto() { - return voto; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/c00253277c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/c00253277c490011162d85db97d6efcd deleted file mode 100644 index 5b8855b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/c00253277c490011162d85db97d6efcd +++ /dev/null @@ -1,39 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - Collections.sort(giochi); - - - - } - - private void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/00c278e877490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/00c278e877490011162d85db97d6efcd deleted file mode 100644 index f9e315a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/00c278e877490011162d85db97d6efcd +++ /dev/null @@ -1,16 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco2 implements Comparator { - - /** - * Ordina dal voto più alto al più basso - * @return - */ - @Override - public int compare(Videogioco v1, Videogioco v2) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/80c5163b77490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/80c5163b77490011162d85db97d6efcd deleted file mode 100644 index 860ddb6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/80c5163b77490011162d85db97d6efcd +++ /dev/null @@ -1,15 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco1 implements Comparator { - - /** - * Ordina i videogiochi per titolo alfabetico crescente - */ - @Override - public int compareTo(Videogioco v1, Videogioco v2) { - return String.compareTo(); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/80cdfcd74b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/80cdfcd74b490011162d85db97d6efcd deleted file mode 100644 index cdd536f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/80cdfcd74b490011162d85db97d6efcd +++ /dev/null @@ -1,27 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iterator(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/b027310b78490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/b027310b78490011162d85db97d6efcd deleted file mode 100644 index 2aa7685..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/b027310b78490011162d85db97d6efcd +++ /dev/null @@ -1,12 +0,0 @@ -package comparatori.videogioco; - -import java.util.Comparator; - -public class ComparatorVideogioco3 implements Comparator { - - @Override - public int compare() { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/008786e85e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/008786e85e490011162d85db97d6efcd deleted file mode 100644 index 75eba6f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/008786e85e490011162d85db97d6efcd +++ /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 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return n - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/60a079f47c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/60a079f47c490011162d85db97d6efcd deleted file mode 100644 index 169d26f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/60a079f47c490011162d85db97d6efcd +++ /dev/null @@ -1,42 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - Collections.sort(giochi); - printList(giochi); - - // Ordina i videogiochi per titolo alfabetico crescente - Collections.sort(giochi, new ComparatorVideogioco1()); - printList(giochi); - - } - - private static void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/f047b1f57a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/f047b1f57a490011162d85db97d6efcd deleted file mode 100644 index 3841e57..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/f047b1f57a490011162d85db97d6efcd +++ /dev/null @@ -1,25 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/905fbcfe77490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/905fbcfe77490011162d85db97d6efcd deleted file mode 100644 index 1e5a494..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/905fbcfe77490011162d85db97d6efcd +++ /dev/null @@ -1,5 +0,0 @@ -package comparatori.videogioco; - -public class ComparatorVideogioco3 { - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/f0027c137d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/f0027c137d490011162d85db97d6efcd deleted file mode 100644 index 8981a79..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/f0027c137d490011162d85db97d6efcd +++ /dev/null @@ -1,49 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Ordina mediante Comparable - compareTo & stampa - System.out.println("ordina per prezzo crescente"); - Collections.sort(giochi); - printList(giochi); - - // Ordina i videogiochi per titolo alfabetico crescente - System.out.println("Ordina i videogiochi per titolo alfabetico crescente"); - Collections.sort(giochi, new ComparatorVideogioco1()); - printList(giochi); - - // Ordina dal voto più alto al più basso - System.out.println("Ordina dal voto più alto al più basso"); - Collections.sort(giochi, new ComparatorVideogioco2()); - printList(giochi); - - } - - private static void printList(List lista) { - System.out.print("{"); - for (int i = 0; i < lista.size(); i++) { - System.out.print(" ["); - System.out.print(lista.get(i).toString()); - System.out.print("] "); - } - System.out.print("}"); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f2/304b6bdc4b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/304b6bdc4b490011162d85db97d6efcd deleted file mode 100644 index cdd536f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f2/304b6bdc4b490011162d85db97d6efcd +++ /dev/null @@ -1,27 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iterator(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f2/b0235aab62490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/b0235aab62490011162d85db97d6efcd deleted file mode 100644 index 24abebd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f2/b0235aab62490011162d85db97d6efcd +++ /dev/null @@ -1,514 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (node.hasLeft() && node.hasRight()) return; // Se il nodo ha sia il figlio sinstro che destro, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - BinaryNode copy; - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - copy = new BinaryNode(node.getRight().getData()); - node.setLeft(copy); - copy.setParentAsLeftChild(node); - } else { - copy = new BinaryNode(node.getLeft().getData()); - node.setRight(copy); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f3/b0e1dac24d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/b0e1dac24d490011162d85db97d6efcd deleted file mode 100644 index 8d88711..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f3/b0e1dac24d490011162d85db97d6efcd +++ /dev/null @@ -1,53 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - public static Integer maxValue(BinaryTree btree) { - Iterator iterator = btree.iteratorInOrder(); - if (!iterator.hasNext()) return null; - Integer maxValue = iterator.next(); - while (iterator.hasNext()) { - Integer current = iterator.next(); - if (current > maxValue) maxValue = current; - } - return maxValue; - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f5/605de02776490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f5/605de02776490011162d85db97d6efcd deleted file mode 100644 index 7bc0706..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f5/605de02776490011162d85db97d6efcd +++ /dev/null @@ -1,49 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } - - public Integer annoUscita() { - return annoUscita; - } - - public Double prezzo() { - return prezzo; - } - - public Integer voto() { - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f6/001eb8c27a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f6/001eb8c27a490011162d85db97d6efcd deleted file mode 100644 index c06a1a5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f6/001eb8c27a490011162d85db97d6efcd +++ /dev/null @@ -1,13 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f6/00f8c70b4d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f6/00f8c70b4d490011162d85db97d6efcd deleted file mode 100644 index ed9f584..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f6/00f8c70b4d490011162d85db97d6efcd +++ /dev/null @@ -1,42 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. - */ - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/5019a0a163490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/5019a0a163490011162d85db97d6efcd deleted file mode 100644 index cf858cf..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/5019a0a163490011162d85db97d6efcd +++ /dev/null @@ -1,519 +0,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() { - return numberLeaf(root); - } - - public int numberLeaf(BinaryNode node) { - if (node == null) return 0; - if (node.getLeft() == null && node.getRight() == null) return 1; - - int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); - int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); - - return nLeft + nRight; - } - - /* - * Si aggiunga alla classe LinkedBinaryTree un metodo che trasforma l'albero corrente - * in modo che ogni suo nodo interno abbia esattamente due figli. Se un nodo ha solo un - * figlio, si aggiunga l'altro nodo figlio come copia del figlio esistente - */ - public void makeFull() { - makeFull(root); - } - - public void makeFull(BinaryNode node) { - if (node == null) return; // Se il nodo è nullo, non serve fare operazioni - if (!node.hasLeft() && !node.hasRight()) return; // Se il nodo è una foglia non ha bisogno di fare operazioni - - makeFull(node.getLeft()); - makeFull(node.getRight()); - - BinaryNode copy; - - if (!node.hasLeft()) { - // Si copia il figlio destro in quello sinistro - copy = new BinaryNode(node.getRight().getData()); - node.setLeft(copy); - copy.setParentAsLeftChild(node); - size++; - } - if (!node.hasRight()) { - copy = new BinaryNode(node.getLeft().getData()); - node.setRight(copy); - copy.setParentAsRightChild(node); - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/d0fa5a8c75490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/d0fa5a8c75490011162d85db97d6efcd deleted file mode 100644 index 991815b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/d0fa5a8c75490011162d85db97d6efcd +++ /dev/null @@ -1,12 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco { - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/7036d1c67f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/7036d1c67f490011162d85db97d6efcd deleted file mode 100644 index 6023964..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/7036d1c67f490011162d85db97d6efcd +++ /dev/null @@ -1,30 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; - -public class InteriRipetuti { - - public static void main(String[] Args) { - - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println(); - } - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/10214a827a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/10214a827a490011162d85db97d6efcd deleted file mode 100644 index e69de29..0000000 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/10aedfc37a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/10aedfc37a490011162d85db97d6efcd deleted file mode 100644 index ca3b4b2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/10aedfc37a490011162d85db97d6efcd +++ /dev/null @@ -1,15 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - // Per adoperare il comparable - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/a08ffa6e4d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/a08ffa6e4d490011162d85db97d6efcd deleted file mode 100644 index 0981778..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/a08ffa6e4d490011162d85db97d6efcd +++ /dev/null @@ -1,51 +0,0 @@ -package binary_tree; - -import java.util.Iterator; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - /* - * Spiegazione, in questo caso, non stiamo operando direttamente - * dall'interno dell'albero, quindi possiamo sfruttare gli iteratori. - * Essendo che dobbiamo per forza verificare tutit i vari nodi possiamo - * adoperare qualsiasi tipo di iteratore. - */ - public static boolean isEvenBtree(BinaryTree btree) { - boolean even = true; - Iterator iterator = btree.iteratorInOrder(); - while (iterator.hasNext() && even) { - Integer current = iterator.next(); - if ((current <= 0) || !(current % 2 == 0)) even = false; - } - return even; - } - - /* - * Esercizio 2 - * Realizzare il metodo statico public static Integer MaxValue(BinaryTree btree) - * che restituisce il riferimento all'oggetto intero più grande - * contenuto in btree. - */ - /* - * Spiegazione, in questo caso, è necessario e sufficiente iterare tutti gli elementi per - * verificare il maggiore. Non essendoci un vero ordinamento sull'albero siamo obbligati a - * iterare tutti i nodi. - */ - public static Integer maxValue(BinaryTree btree) { - Iterator iterator = btree.iteratorInOrder(); - if (!iterator.hasNext()) return null; - Integer maxValue = iterator.next(); - while (iterator.hasNext()) { - - } - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fc/e0c897f14a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/e0c897f14a490011162d85db97d6efcd deleted file mode 100644 index 4322605..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fc/e0c897f14a490011162d85db97d6efcd +++ /dev/null @@ -1,15 +0,0 @@ -package binary_tree; - -public class ExerciseBinaryTree { - - - /* - * Esercizio 1 II parziale 10.01.2018 - * Un albero binario di interi si dice pari se tutti i nodi contengono - * un intero positivo pari. Realizzare il metodo statico - * public static boolean IsEvenBtree (BinaryTree btree) - * che verifica se l'albero binario dato btree e' pari. - */ - - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fd/204b1a5976490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/204b1a5976490011162d85db97d6efcd deleted file mode 100644 index f79a397..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fd/204b1a5976490011162d85db97d6efcd +++ /dev/null @@ -1,57 +0,0 @@ -package comparatori.videogioco; - -public class Videogioco implements Comparable{ - - // Campi di istanza - private String titolo; - private String piattaforma; - private Integer annoUscita; - private Double prezzo; - private Integer voto; - - // Costruttore - - private Videogioco() {} - - public Videogioco( - String titolo, - String piattaforma, - Integer annoUscita, - Double prezzo, - Integer voto) { - this.titolo = (titolo == null) ? "" : titolo.trim(); - this.piattaforma = (piattaforma == null) ? "" : piattaforma.trim(); - this.annoUscita = (annoUscita == null) ? 0 : annoUscita; - this.prezzo = (prezzo == null) ? 0.0 : prezzo; - this.voto = (voto == null) ? 0 : voto; - } - - // Getter - public String titolo() { - return titolo; - } - - public String piattaforma() { - return piattaforma; - } - - public Integer annoUscita() { - return annoUscita; - } - - public Double prezzo() { - return prezzo; - } - - public Integer voto() { - return voto; - } - - // Implementazione del metodo comparatore - - /** - * compareTo ordina per prezzo crescente - */ - @Override - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fd/b0b1616180490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/b0b1616180490011162d85db97d6efcd deleted file mode 100644 index b395512..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fd/b0b1616180490011162d85db97d6efcd +++ /dev/null @@ -1,44 +0,0 @@ -package jcf_set.example; - -import java.util.HashSet; -import java.util.Random; -import java.util.Set; -import java.util.TreeSet; - -public class InteriRipetuti { - - public static void main(String[] Args) { - new InteriRipetuti().run(); - } - - public void run() { - int N = 20; - int MAX_INT = 50; - - // HASHSET (non ordinato) - Set hashset = new HashSet(); - - // Genero i numeri casuali inserendoli in nel set di hashset - Random generator = new Random(); - for (int i = 0; i < N; i++) { - Integer x = generator.nextInt(MAX_INT + 1); - if (!hashset.add(x)) { - System.out.println("[NON INSERITO] Causa: Intero ripetuto: " + x); - } else System.out.println("Intero inserito: " + x); - } - - // Stampa di hashset - System.out.println("Hashset"); - for (Integer x : hashset) System.out.println(x + " "); - System.out.println(); System.out.println(hashset); - - // TREESET (ordinato) - Set treeset = new TreeSet(hashset); - - // Stampa di treeset - System.out.println("Treeset"); - for (Integer x : treeser) System.out.println(x + " "); - System.out.println(); System.out.println(hashset); - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fe/60c3bc217b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fe/60c3bc217b490011162d85db97d6efcd deleted file mode 100644 index cf4812d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fe/60c3bc217b490011162d85db97d6efcd +++ /dev/null @@ -1,27 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - Collections.sort(); - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fe/c02b7e3d7b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fe/c02b7e3d7b490011162d85db97d6efcd deleted file mode 100644 index a9f4894..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fe/c02b7e3d7b490011162d85db97d6efcd +++ /dev/null @@ -1,32 +0,0 @@ -package comparatori.videogioco; - -import java.util.ArrayList; -import java.util.Collections; - -public class Main { - - public static void main(String[] Args) { - - ArrayList giochi = new ArrayList(); - - giochi.add(new Videogioco("The Last of Us Part II", "PS5", 2020, 39.99, 9)); - giochi.add(new Videogioco("Cyberpunk 2077", "PC", 2020, 29.99, 8)); - giochi.add(new Videogioco("Minecraft", "PC", 2011, 19.99, 10)); - giochi.add(new Videogioco("Ghost of Tsushima", "PS5", 2020, 49.99, 9)); - giochi.add(new Videogioco("Resident Evil 4 Remake", "PS5", 2023, 44.99, 9)); - giochi.add(new Videogioco("GTA V", "PC", 2013, 14.99, 8)); - giochi.add(new Videogioco("God of War Ragnarok", "PS5", 2022, 59.99, 10)); - giochi.add(new Videogioco("Fortnite", "PC", 2017, 0.0, 7)); - - // Per adoperare il comparable - Collections.sort(giochi); - - - - } - - private T void printList(List ) { - - } - -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.location b/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.location index 7be58fc..51c5d9d 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.location and b/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.location differ 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 deleted file mode 100644 index 08e6b18..0000000 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.markers.snap and /dev/null 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 deleted file mode 100644 index 08e6b18..0000000 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.syncinfo.snap and /dev/null differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/90/96/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/90/96/history.index index 3a50960..3f854e9 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/90/96/history.index and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/90/96/history.index differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/ad/a2/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/ad/a2/history.index index de5271e..14e2a1f 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/ad/a2/history.index and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/ad/a2/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 deleted file mode 100644 index 190fcdf..0000000 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/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 ec7fd6a..9e62d61 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 deleted file mode 100644 index f5044ff..0000000 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.markers.snap and /dev/null 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 deleted file mode 100644 index 08e6b18..0000000 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.syncinfo.snap and /dev/null 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 cf87a86..a0af7b2 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 deleted file mode 100644 index 08e6b18..0000000 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.root/.markers.snap and /dev/null differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.root/3.tree b/.metadata/.plugins/org.eclipse.core.resources/.root/3.tree deleted file mode 100644 index 8d27d6e..0000000 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.root/3.tree and /dev/null 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 6dffc96..df10c76 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/3.snap b/.metadata/.plugins/org.eclipse.core.resources/3.snap deleted file mode 100644 index 14d24e9..0000000 Binary files a/.metadata/.plugins/org.eclipse.core.resources/3.snap and /dev/null differ diff --git a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.debug.ui.prefs b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.debug.ui.prefs index 5f4fc2f..a2e2ec3 100644 --- a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.debug.ui.prefs +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.debug.ui.prefs @@ -1,20 +1,3 @@ eclipse.preferences.version=1 -org.eclipse.debug.ui.MemoryHistoryKnownColor=235,235,235 -org.eclipse.debug.ui.MemoryHistoryKnownColor,defaultValueBeforeOverriddenFromCSS=0,0,0 -org.eclipse.debug.ui.MemoryHistoryUnknownColor=170,175,185 -org.eclipse.debug.ui.MemoryHistoryUnknownColor,defaultValueBeforeOverriddenFromCSS=114,119,129 -org.eclipse.debug.ui.PREF_CHANGED_VALUE_BACKGROUND=150,80,115 -org.eclipse.debug.ui.PREF_CHANGED_VALUE_BACKGROUND,defaultValueBeforeOverriddenFromCSS=255,255,0 -org.eclipse.debug.ui.PREF_LAUNCH_PERSPECTIVES=\r\n\r\n -org.eclipse.debug.ui.changedDebugElement=255,128,128 -org.eclipse.debug.ui.changedDebugElement,defaultValueBeforeOverriddenFromCSS=255,0,0 -org.eclipse.debug.ui.consoleBackground=53,53,53 -org.eclipse.debug.ui.consoleBackground,defaultValueBeforeOverriddenFromCSS=255,255,255 -org.eclipse.debug.ui.errorColor=225,30,70 -org.eclipse.debug.ui.errorColor,defaultValueBeforeOverriddenFromCSS=255,0,0 -org.eclipse.debug.ui.inColor=140,175,210 -org.eclipse.debug.ui.inColor,defaultValueBeforeOverriddenFromCSS=0,200,125 -org.eclipse.debug.ui.outColor=235,235,235 -org.eclipse.debug.ui.outColor,defaultValueBeforeOverriddenFromCSS=0,0,0 -overriddenByCSS=,org.eclipse.debug.ui.MemoryHistoryKnownColor,org.eclipse.debug.ui.MemoryHistoryUnknownColor,org.eclipse.debug.ui.PREF_CHANGED_VALUE_BACKGROUND,org.eclipse.debug.ui.changedDebugElement,org.eclipse.debug.ui.consoleBackground,org.eclipse.debug.ui.errorColor,org.eclipse.debug.ui.inColor,org.eclipse.debug.ui.outColor, +org.eclipse.debug.ui.PREF_LAUNCH_PERSPECTIVES=\n\n preferredTargets=default\:default| 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 ed2fe85..041f359 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\r\n +org.eclipse.jdt.launching.PREF_VM_XML=\n\n \n \n \n \n\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..a59e6f0 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 @@ -1,158 +1,9 @@ content_assist_autoactivation=false -content_assist_completion_replacement_background=200,200,0 -content_assist_completion_replacement_background,defaultValueBeforeOverriddenFromCSS=255,255,0 -content_assist_completion_replacement_foreground=200,0,0 -content_assist_completion_replacement_foreground,defaultValueBeforeOverriddenFromCSS=255,0,0 content_assist_number_of_computers=15 -content_assist_parameters_background=52,57,61 -content_assist_parameters_background,defaultValueBeforeOverriddenFromCSS=255,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 -java_comment_task_tag=154,140,124 -java_comment_task_tag,defaultValueBeforeOverriddenFromCSS=127,159,191 -java_default=217,232,247 -java_default,defaultValueBeforeOverriddenFromCSS=0,0,0 -java_doc_default=128,128,128 -java_doc_default,defaultValueBeforeOverriddenFromCSS=63,95,191 -java_doc_keyword=154,140,124 -java_doc_keyword,defaultValueBeforeOverriddenFromCSS=127,159,191 -java_doc_link=169,156,140 -java_doc_link,defaultValueBeforeOverriddenFromCSS=63,63,191 -java_doc_tag=30,120,155 -java_doc_tag,defaultValueBeforeOverriddenFromCSS=127,127,159 -java_keyword=204,108,29 -java_keyword,defaultValueBeforeOverriddenFromCSS=127,0,85 -java_keyword_bold=false -java_keyword_bold,defaultValueBeforeOverriddenFromCSS=true -java_keyword_return=204,108,29 -java_keyword_return,defaultValueBeforeOverriddenFromCSS=127,0,85 -java_keyword_return_bold=false -java_keyword_return_bold,defaultValueBeforeOverriddenFromCSS=true -java_multi_line_comment=128,128,128 -java_multi_line_comment,defaultValueBeforeOverriddenFromCSS=63,127,95 -java_operator=230,230,250 -java_operator,defaultValueBeforeOverriddenFromCSS=0,0,0 -java_single_line_comment=128,128,128 -java_single_line_comment,defaultValueBeforeOverriddenFromCSS=63,127,95 -java_string=23,198,163 -java_string,defaultValueBeforeOverriddenFromCSS=42,0,255 -javadocElementsStyling.darkModeDefaultColors=true -matchingBracketsColor=249,250,244 -matchingBracketsColor,defaultValueBeforeOverriddenFromCSS=127,0,85 org.eclipse.jdt.ui.formatterprofiles.version=23 -overriddenByCSS=,content_assist_completion_replacement_background,content_assist_completion_replacement_foreground,content_assist_parameters_background,content_assist_parameters_foreground,java_bracket,java_comment_task_tag,java_default,java_doc_default,java_doc_keyword,java_doc_link,java_doc_tag,java_keyword,java_keyword_bold,java_keyword_return,java_keyword_return_bold,java_multi_line_comment,java_operator,java_single_line_comment,java_string,matchingBracketsColor,pf_coloring_argument,pf_coloring_assignment,pf_coloring_comment,pf_coloring_key,pf_coloring_value,semanticHighlighting.abstractClass.color,semanticHighlighting.abstractClass.enabled,semanticHighlighting.abstractMethodInvocation.color,semanticHighlighting.abstractMethodInvocation.enabled,semanticHighlighting.annotation.color,semanticHighlighting.annotation.enabled,semanticHighlighting.annotation.italic,semanticHighlighting.annotationElementReference.color,semanticHighlighting.annotationElementReference.enabled,semanticHighlighting.class.color,semanticHighlighting.class.enabled,semanticHighlighting.deprecatedMember.color,semanticHighlighting.deprecatedMember.enabled,semanticHighlighting.deprecatedMember.underline,semanticHighlighting.deprecatedMember.strikethrough,semanticHighlighting.enum.color,semanticHighlighting.enum.enabled,semanticHighlighting.enum.italic,semanticHighlighting.field.color,semanticHighlighting.field.enabled,semanticHighlighting.inheritedField.color,semanticHighlighting.inheritedMethodInvocation.color,semanticHighlighting.inheritedMethodInvocation.enabled,semanticHighlighting.interface.color,semanticHighlighting.interface.enabled,semanticHighlighting.localVariable.color,semanticHighlighting.localVariable.enabled,semanticHighlighting.localVariableDeclaration.color,semanticHighlighting.localVariableDeclaration.enabled,semanticHighlighting.localVariableDeclaration.bold,semanticHighlighting.method.color,semanticHighlighting.method.enabled,semanticHighlighting.methodDeclarationName.color,semanticHighlighting.methodDeclarationName.enabled,semanticHighlighting.methodDeclarationName.bold,semanticHighlighting.number.color,semanticHighlighting.number.enabled,semanticHighlighting.parameterVariable.color,semanticHighlighting.parameterVariable.enabled,semanticHighlighting.staticField.color,semanticHighlighting.staticField.enabled,semanticHighlighting.staticFinalField.color,semanticHighlighting.staticFinalField.enabled,semanticHighlighting.staticMethodInvocation.color,semanticHighlighting.staticMethodInvocation.enabled,semanticHighlighting.typeArgument.color,semanticHighlighting.typeArgument.enabled,semanticHighlighting.typeParameter.color,semanticHighlighting.typeParameter.enabled,semanticHighlighting.typeParameter.bold,semanticHighlighting.restrictedKeywords.color,semanticHighlighting.restrictedKeywords.bold,javadocElementsStyling.darkModeDefaultColors, -pf_coloring_argument=221,40,103 -pf_coloring_argument,defaultValueBeforeOverriddenFromCSS=127,0,85 -pf_coloring_assignment=217,232,247 -pf_coloring_assignment,defaultValueBeforeOverriddenFromCSS=0,0,0 -pf_coloring_comment=128,128,128 -pf_coloring_comment,defaultValueBeforeOverriddenFromCSS=63,127,95 -pf_coloring_key=217,232,247 -pf_coloring_key,defaultValueBeforeOverriddenFromCSS=0,0,0 -pf_coloring_value=23,198,163 -pf_coloring_value,defaultValueBeforeOverriddenFromCSS=42,0,255 -semanticHighlighting.abstractClass.color=62,171,230 -semanticHighlighting.abstractClass.color,defaultValueBeforeOverriddenFromCSS=139,136,22 -semanticHighlighting.abstractClass.enabled=true -semanticHighlighting.abstractClass.enabled,defaultValueBeforeOverriddenFromCSS=false -semanticHighlighting.abstractMethodInvocation.color=128,246,167 -semanticHighlighting.abstractMethodInvocation.color,defaultValueBeforeOverriddenFromCSS=0,0,0 -semanticHighlighting.abstractMethodInvocation.enabled=true -semanticHighlighting.abstractMethodInvocation.enabled,defaultValueBeforeOverriddenFromCSS=false -semanticHighlighting.annotation.color=160,160,160 -semanticHighlighting.annotation.color,defaultValueBeforeOverriddenFromCSS=100,100,100 -semanticHighlighting.annotation.enabled=true -semanticHighlighting.annotation.italic=true -semanticHighlighting.annotation.italic,defaultValueBeforeOverriddenFromCSS=false -semanticHighlighting.annotationElementReference.color=235,75,100 -semanticHighlighting.annotationElementReference.color,defaultValueBeforeOverriddenFromCSS=0,0,0 -semanticHighlighting.annotationElementReference.enabled=true -semanticHighlighting.annotationElementReference.enabled,defaultValueBeforeOverriddenFromCSS=false -semanticHighlighting.class.color=18,144,195 -semanticHighlighting.class.color,defaultValueBeforeOverriddenFromCSS=0,80,50 -semanticHighlighting.class.enabled=true -semanticHighlighting.class.enabled,defaultValueBeforeOverriddenFromCSS=false -semanticHighlighting.deprecatedMember.color=128,128,128 -semanticHighlighting.deprecatedMember.color,defaultValueBeforeOverriddenFromCSS=0,0,0 -semanticHighlighting.deprecatedMember.enabled=true -semanticHighlighting.deprecatedMember.strikethrough=true -semanticHighlighting.deprecatedMember.underline=false -semanticHighlighting.enum.color=204,129,186 -semanticHighlighting.enum.color,defaultValueBeforeOverriddenFromCSS=100,70,50 -semanticHighlighting.enum.enabled=true -semanticHighlighting.enum.enabled,defaultValueBeforeOverriddenFromCSS=false -semanticHighlighting.enum.italic=true -semanticHighlighting.enum.italic,defaultValueBeforeOverriddenFromCSS=false -semanticHighlighting.field.color=102,225,248 -semanticHighlighting.field.color,defaultValueBeforeOverriddenFromCSS=0,0,192 -semanticHighlighting.field.enabled=true -semanticHighlighting.inheritedField.color=143,143,191 -semanticHighlighting.inheritedField.color,defaultValueBeforeOverriddenFromCSS=0,0,192 -semanticHighlighting.inheritedMethodInvocation.color=205,246,104 -semanticHighlighting.inheritedMethodInvocation.color,defaultValueBeforeOverriddenFromCSS=0,0,0 -semanticHighlighting.inheritedMethodInvocation.enabled=true -semanticHighlighting.inheritedMethodInvocation.enabled,defaultValueBeforeOverriddenFromCSS=false -semanticHighlighting.interface.color=128,242,246 -semanticHighlighting.interface.color,defaultValueBeforeOverriddenFromCSS=50,63,112 -semanticHighlighting.interface.enabled=true -semanticHighlighting.interface.enabled,defaultValueBeforeOverriddenFromCSS=false -semanticHighlighting.localVariable.color=243,236,121 -semanticHighlighting.localVariable.color,defaultValueBeforeOverriddenFromCSS=106,62,62 -semanticHighlighting.localVariable.enabled=true -semanticHighlighting.localVariableDeclaration.bold=false -semanticHighlighting.localVariableDeclaration.bold,defaultValueBeforeOverriddenFromCSS=true -semanticHighlighting.localVariableDeclaration.color=242,242,0 -semanticHighlighting.localVariableDeclaration.color,defaultValueBeforeOverriddenFromCSS=106,62,62 -semanticHighlighting.localVariableDeclaration.enabled=true -semanticHighlighting.localVariableDeclaration.enabled,defaultValueBeforeOverriddenFromCSS=false -semanticHighlighting.method.color=167,236,33 -semanticHighlighting.method.color,defaultValueBeforeOverriddenFromCSS=0,0,0 -semanticHighlighting.method.enabled=true -semanticHighlighting.method.enabled,defaultValueBeforeOverriddenFromCSS=false -semanticHighlighting.methodDeclarationName.bold=false -semanticHighlighting.methodDeclarationName.bold,defaultValueBeforeOverriddenFromCSS=true -semanticHighlighting.methodDeclarationName.color=30,181,64 -semanticHighlighting.methodDeclarationName.color,defaultValueBeforeOverriddenFromCSS=0,0,0 -semanticHighlighting.methodDeclarationName.enabled=true -semanticHighlighting.methodDeclarationName.enabled,defaultValueBeforeOverriddenFromCSS=false -semanticHighlighting.number.color=104,151,187 -semanticHighlighting.number.color,defaultValueBeforeOverriddenFromCSS=42,0,255 -semanticHighlighting.number.enabled=true -semanticHighlighting.number.enabled,defaultValueBeforeOverriddenFromCSS=false -semanticHighlighting.parameterVariable.color=121,171,255 -semanticHighlighting.parameterVariable.color,defaultValueBeforeOverriddenFromCSS=106,62,62 -semanticHighlighting.parameterVariable.enabled=true -semanticHighlighting.parameterVariable.enabled,defaultValueBeforeOverriddenFromCSS=false -semanticHighlighting.restrictedKeywords.bold=false -semanticHighlighting.restrictedKeywords.bold,defaultValueBeforeOverriddenFromCSS=true -semanticHighlighting.restrictedKeywords.color=204,108,29 -semanticHighlighting.restrictedKeywords.color,defaultValueBeforeOverriddenFromCSS=127,0,85 -semanticHighlighting.staticField.color=141,218,248 -semanticHighlighting.staticField.color,defaultValueBeforeOverriddenFromCSS=0,0,192 -semanticHighlighting.staticField.enabled=true -semanticHighlighting.staticFinalField.color=141,218,248 -semanticHighlighting.staticFinalField.color,defaultValueBeforeOverriddenFromCSS=0,0,192 -semanticHighlighting.staticFinalField.enabled=true -semanticHighlighting.staticMethodInvocation.color=150,236,63 -semanticHighlighting.staticMethodInvocation.color,defaultValueBeforeOverriddenFromCSS=0,0,0 -semanticHighlighting.staticMethodInvocation.enabled=true -semanticHighlighting.typeArgument.color=177,102,218 -semanticHighlighting.typeArgument.color,defaultValueBeforeOverriddenFromCSS=13,100,0 -semanticHighlighting.typeArgument.enabled=true -semanticHighlighting.typeArgument.enabled,defaultValueBeforeOverriddenFromCSS=false -semanticHighlighting.typeParameter.bold=false -semanticHighlighting.typeParameter.bold,defaultValueBeforeOverriddenFromCSS=true -semanticHighlighting.typeParameter.color=191,164,164 -semanticHighlighting.typeParameter.color,defaultValueBeforeOverriddenFromCSS=100,70,50 -semanticHighlighting.typeParameter.enabled=true -semanticHighlighting.typeParameter.enabled,defaultValueBeforeOverriddenFromCSS=false sourceHoverBackgroundColor=30,31,34 spelling_ignore_ampersand_in_properties=true spelling_ignore_digits=true 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 6a24591..489e2c7 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=949639751129723 +platformState=943974497357990 quickStart=false tipsAndTricks=true diff --git a/.metadata/.plugins/org.eclipse.debug.ui/launchConfigurationHistory.xml b/.metadata/.plugins/org.eclipse.debug.ui/launchConfigurationHistory.xml index 9e8cc5d..30950b3 100644 --- a/.metadata/.plugins/org.eclipse.debug.ui/launchConfigurationHistory.xml +++ b/.metadata/.plugins/org.eclipse.debug.ui/launchConfigurationHistory.xml @@ -2,16 +2,16 @@ - - - - - - - - - - + + + + + + + + + + @@ -21,16 +21,16 @@ - - - - - - - - - - + + + + + + + + + + @@ -40,16 +40,16 @@ - - - - - - - - - - + + + + + + + + + + diff --git a/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi b/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi index 0448152..c3ca6b7 100644 --- a/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi +++ b/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi @@ -1,13 +1,13 @@ - + activeSchemeId:org.eclipse.ui.defaultAcceleratorConfiguration - + - - + + topLevel shellMaximized @@ -76,10 +76,10 @@ persp.viewSC:org.eclipse.ant.ui.views.AntView persp.editorOnboardingImageUri:platform:/plugin/org.eclipse.jdt.ui/$nl$/icons/full/onboarding_jperspective.svg persp.editorOnboardingText:Open a file or drop files here to open them. - persp.editorOnboardingCommand:Find Actions$$$Ctrl+3 - persp.editorOnboardingCommand:Show Key Assist$$$Ctrl+Shift+L - persp.editorOnboardingCommand:New$$$Ctrl+N - persp.editorOnboardingCommand:Open Type$$$Ctrl+Shift+T + persp.editorOnboardingCommand:Find Actions$$$⌘3 + persp.editorOnboardingCommand:Show Key Assist$$$⇧⌘L + persp.editorOnboardingCommand:New$$$⌘N + persp.editorOnboardingCommand:Open Type$$$⇧⌘T @@ -140,10 +140,11 @@ - + org.eclipse.e4.secondaryDataStack Oomph Java + Minimized View categoryTag:General @@ -176,16 +177,6 @@ View categoryTag:General - - View - categoryTag:Oomph - NoRestore - - - View - categoryTag:Oomph - NoRestore - View categoryTag:Java @@ -234,31 +225,55 @@ View categoryTag:Help - - - EditorStack - org.eclipse.e4.primaryDataStack - active - - - Editor - removeOnHide - org.eclipse.jdt.ui.CompilationUnitEditor - active + + + + EditorStack + org.eclipse.e4.primaryDataStack + + + Editor + removeOnHide + org.eclipse.jdt.ui.CompilationUnitEditor + + + + + EditorStack + + + Editor + removeOnHide + org.eclipse.jdt.ui.CompilationUnitEditor + + + + EditorStack + active + noFocus + + + Editor + removeOnHide + org.eclipse.jdt.ui.CompilationUnitEditor + active + activeOnClose + + - + View categoryTag:Java ViewMenu menuContribution:menu - + @@ -275,7 +290,7 @@ - + View categoryTag:General @@ -336,7 +351,7 @@ ViewMenu menuContribution:menu - + @@ -386,36 +401,10 @@ View categoryTag:Ant - - - - - View - categoryTag:Oomph - NoRestore - - ViewMenu - menuContribution:menu - - - - - - - - View - categoryTag:Oomph - NoRestore - - ViewMenu - menuContribution:menu - - - - + View categoryTag:Java @@ -431,7 +420,7 @@ Draggable - + toolbarSeparator @@ -439,8 +428,8 @@ Draggable - - + + toolbarSeparator @@ -464,10 +453,7 @@ Draggable - - - - Draggable + toolbarSeparator @@ -516,7 +502,7 @@ TrimStack Draggable - + TrimStack Draggable @@ -526,6 +512,10 @@ + + + + @@ -540,23 +530,25 @@ - - - - - - - - - - - - - platform:win32 + + platform:cocoa - - - + + platform:cocoa + + + + + + platform:cocoa + + + + + + + + @@ -598,135 +590,223 @@ - - - + + platform:cocoa + - - + + platform:cocoa + - - - - - - - - + + platform:cocoa + - - - + + platform:cocoa + - - - + + platform:cocoa + - - - - - + + platform:cocoa - - + + platform:cocoa + - - - + + platform:cocoa + - - - + + platform:cocoa + - - - - - - - - - + + platform:cocoa + - - + + platform:cocoa + - - + + platform:cocoa + - - - - - - - - - - - - + + platform:cocoa + - - - - + + platform:cocoa + - - + + platform:cocoa + - - - - - - - - - - - - - - + + platform:cocoa + - - + + + + + + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + + + platform:cocoa + + + + + + platform:cocoa + + + platform:cocoa + + + + platform:cocoa + + + platform:cocoa + + + + + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + + platform:cocoa + + + platform:cocoa + + + + platform:cocoa + + + + + + platform:cocoa + + + + + + + platform:cocoa + + + + + + + + + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + + platform:cocoa + + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + + + + + platform:cocoa + + + + platform:cocoa + + + + + platform:cocoa + + + + platform:cocoa + + + + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -739,82 +819,146 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + platform:cocoa + + + platform:cocoa + + + + + + + platform:cocoa + + + + + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + + platform:cocoa + + + + + + platform:cocoa + + + + + + + + + + + + + + + + platform:cocoa + - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + platform:cocoa + + + platform:cocoa + + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + platform:cocoa + + + + + platform:cocoa + + + + + + + + platform:cocoa + + + + platform:cocoa + + + + + + + + + + @@ -823,10 +967,10 @@ - - - - + + + + @@ -840,7 +984,13 @@ - + + platform:cocoa + + + platform:cocoa + + @@ -848,40 +998,50 @@ - - + + - - platform:win32 - + - - - + + + + + platform:cocoa + + + platform:cocoa + - - - + + platform:cocoa + + + + platform:cocoa + - - - + + platform:cocoa + + + - + @@ -893,8 +1053,8 @@ - - + + @@ -916,44 +1076,48 @@ - + - + + platform:cocoa + - - - + + + - + - - - - - + + + + + - - - + + + - + - + - + - + + + @@ -1465,18 +1629,18 @@ 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 0037c30..a1a811f 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/2555619149.index b/.metadata/.plugins/org.eclipse.jdt.core/2555619149.index deleted file mode 100644 index 1cb75d7..0000000 Binary files a/.metadata/.plugins/org.eclipse.jdt.core/2555619149.index and /dev/null differ diff --git a/.metadata/.plugins/org.eclipse.jdt.core/2876837992.index b/.metadata/.plugins/org.eclipse.jdt.core/2876837992.index deleted file mode 100644 index 900aab8..0000000 Binary files a/.metadata/.plugins/org.eclipse.jdt.core/2876837992.index and /dev/null differ diff --git a/.metadata/.plugins/org.eclipse.jdt.core/externalFilesCache b/.metadata/.plugins/org.eclipse.jdt.core/externalFilesCache index f3be2a1..5119c62 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/externalLibsTimeStamps b/.metadata/.plugins/org.eclipse.jdt.core/externalLibsTimeStamps index 7dfad37..a6ddeb1 100644 Binary files a/.metadata/.plugins/org.eclipse.jdt.core/externalLibsTimeStamps and b/.metadata/.plugins/org.eclipse.jdt.core/externalLibsTimeStamps differ diff --git a/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt b/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt index c1bda67..2a75bdd 100644 --- a/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt +++ b/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt @@ -1,3 +1,3 @@ -INDEX VERSION 1.134+C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.jdt.core -2555619149.index +INDEX VERSION 1.134+/Users/eslusarz/eclipse-workspace/.metadata/.plugins/org.eclipse.jdt.core +353077879.index 1865797976.index diff --git a/.metadata/.plugins/org.eclipse.jdt.core/variablesAndContainers.dat b/.metadata/.plugins/org.eclipse.jdt.core/variablesAndContainers.dat index 010e80f..737f603 100644 Binary files a/.metadata/.plugins/org.eclipse.jdt.core/variablesAndContainers.dat and b/.metadata/.plugins/org.eclipse.jdt.core/variablesAndContainers.dat differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/dialog_settings.xml b/.metadata/.plugins/org.eclipse.jdt.ui/dialog_settings.xml index 678f1b1..681b465 100644 --- a/.metadata/.plugins/org.eclipse.jdt.ui/dialog_settings.xml +++ b/.metadata/.plugins/org.eclipse.jdt.ui/dialog_settings.xml @@ -6,12 +6,14 @@ + +
- +
@@ -22,9 +24,9 @@
- + - +
@@ -32,9 +34,9 @@
- - - + + +
@@ -51,4 +53,11 @@
+
+ + + + + +
diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png deleted file mode 100644 index 6630b18..0000000 Binary files a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png and /dev/null differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/1.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/1.png deleted file mode 100644 index 469abaa..0000000 Binary files a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/1.png and /dev/null 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 deleted file mode 100644 index dfbdbb0..0000000 Binary files a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png and /dev/null differ diff --git a/.metadata/.plugins/org.eclipse.m2e.logback/0.log b/.metadata/.plugins/org.eclipse.m2e.logback/0.log index 80a58e3..4a27f66 100644 --- a/.metadata/.plugins/org.eclipse.m2e.logback/0.log +++ b/.metadata/.plugins/org.eclipse.m2e.logback/0.log @@ -5,3 +5,4 @@ 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. 2026-05-10 18:30:16,902 [Worker-6: 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-11 17:51:48,080 [Worker-5: Loading available Gradle versions] INFO o.e.b.c.i.u.g.PublishedGradleVersions - Gradle version information cache is out-of-date. Trying to update. +2026-05-12 15:01:49,578 [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. diff --git a/.metadata/version.ini b/.metadata/version.ini index 8872af9..b387d43 100644 --- a/.metadata/version.ini +++ b/.metadata/version.ini @@ -1,3 +1,3 @@ -#Tue May 12 13:59:16 CEST 2026 +#Tue May 12 15:01:46 CEST 2026 org.eclipse.core.runtime=2 org.eclipse.platform=4.39.0.v20260226-0420 diff --git a/asdl/bin/network/Graph.class b/asdl/bin/network/Graph.class new file mode 100644 index 0000000..71e4e17 Binary files /dev/null and b/asdl/bin/network/Graph.class differ diff --git a/asdl/bin/network/UnweightGraph.class b/asdl/bin/network/UnweightGraph.class new file mode 100644 index 0000000..0e3a792 Binary files /dev/null and b/asdl/bin/network/UnweightGraph.class differ diff --git a/asdl/bin/network/network/Network$BreadthFirstIterator.class b/asdl/bin/network/network/Network$BreadthFirstIterator.class new file mode 100644 index 0000000..b1b4c8b Binary files /dev/null and b/asdl/bin/network/network/Network$BreadthFirstIterator.class differ diff --git a/asdl/bin/network/network/Network$DepthFirstIterator.class b/asdl/bin/network/network/Network$DepthFirstIterator.class new file mode 100644 index 0000000..5a20513 Binary files /dev/null and b/asdl/bin/network/network/Network$DepthFirstIterator.class differ diff --git a/asdl/bin/network/network/Network$NetworkIterator.class b/asdl/bin/network/network/Network$NetworkIterator.class new file mode 100644 index 0000000..9feb5eb Binary files /dev/null and b/asdl/bin/network/network/Network$NetworkIterator.class differ diff --git a/asdl/bin/network/network/Network.class b/asdl/bin/network/network/Network.class new file mode 100644 index 0000000..14d8b28 Binary files /dev/null and b/asdl/bin/network/network/Network.class differ diff --git a/asdl/bin/network/network/UnweightNetwork$BreadthFirstIterator.class b/asdl/bin/network/network/UnweightNetwork$BreadthFirstIterator.class new file mode 100644 index 0000000..24a9c19 Binary files /dev/null and b/asdl/bin/network/network/UnweightNetwork$BreadthFirstIterator.class differ diff --git a/asdl/bin/network/network/UnweightNetwork$DepthFirstIterator.class b/asdl/bin/network/network/UnweightNetwork$DepthFirstIterator.class new file mode 100644 index 0000000..0da675d Binary files /dev/null and b/asdl/bin/network/network/UnweightNetwork$DepthFirstIterator.class differ diff --git a/asdl/bin/network/network/UnweightNetwork$UnweightNetworkIterator.class b/asdl/bin/network/network/UnweightNetwork$UnweightNetworkIterator.class new file mode 100644 index 0000000..0bf3d27 Binary files /dev/null and b/asdl/bin/network/network/UnweightNetwork$UnweightNetworkIterator.class differ diff --git a/asdl/bin/network/network/UnweightNetwork.class b/asdl/bin/network/network/UnweightNetwork.class new file mode 100644 index 0000000..263d1f2 Binary files /dev/null and b/asdl/bin/network/network/UnweightNetwork.class differ diff --git a/asdl/bin/network/undirected_network/UndirectedNetwork.class b/asdl/bin/network/undirected_network/UndirectedNetwork.class new file mode 100644 index 0000000..77dcd8e Binary files /dev/null and b/asdl/bin/network/undirected_network/UndirectedNetwork.class differ diff --git a/asdl/bin/parziale/p231110/Esercizio.class b/asdl/bin/parziale/p231110/Esercizio.class new file mode 100644 index 0000000..db381e8 Binary files /dev/null and b/asdl/bin/parziale/p231110/Esercizio.class differ diff --git a/asdl/bin/parziale/p231110/Libreria$1.class b/asdl/bin/parziale/p231110/Libreria$1.class new file mode 100644 index 0000000..d757784 Binary files /dev/null and b/asdl/bin/parziale/p231110/Libreria$1.class differ diff --git a/asdl/bin/parziale/p231110/Libreria$2.class b/asdl/bin/parziale/p231110/Libreria$2.class new file mode 100644 index 0000000..73f49cf Binary files /dev/null and b/asdl/bin/parziale/p231110/Libreria$2.class differ diff --git a/asdl/bin/parziale/p231110/Libreria.class b/asdl/bin/parziale/p231110/Libreria.class new file mode 100644 index 0000000..bb7d69b Binary files /dev/null and b/asdl/bin/parziale/p231110/Libreria.class differ diff --git a/asdl/bin/parziale/p231110/Libro.class b/asdl/bin/parziale/p231110/Libro.class new file mode 100644 index 0000000..98660a7 Binary files /dev/null and b/asdl/bin/parziale/p231110/Libro.class differ diff --git a/asdl/src/network/Graph.java b/asdl/src/network/Graph.java new file mode 100644 index 0000000..b00f70a --- /dev/null +++ b/asdl/src/network/Graph.java @@ -0,0 +1,36 @@ +package network; + +import java.util.Iterator; +import java.util.Set; + +public interface Graph extends Iterable { + + public boolean containsVertex(Vertex vertex); + + public boolean addVertex(Vertex vertex); + + public boolean removeVertex(Vertex vertex); + + public int edgeSize(); + + public Wheight getEdgeWheight(Vertex v1, Vertex v2); + + public boolean containsEdge(Vertex v1, Vertex v2); + + public boolean addEdge(Vertex v1, Vertex v2, Double weight); + + public boolean removeEdge(Vertex v1, Vertex v2); + + public boolean isEmpty(); + + public int size(); + + public Set neighbors (Vertex v); + + public Iterator iterator(); + + public Iterator breadthFirstIterator(Vertex v); + + public Iterator depthFisrtIterator (Vertex v); + +} diff --git a/asdl/src/network/UnweightGraph.java b/asdl/src/network/UnweightGraph.java new file mode 100644 index 0000000..dd40cf0 --- /dev/null +++ b/asdl/src/network/UnweightGraph.java @@ -0,0 +1,47 @@ +package network; + +import java.util.Iterator; +import java.util.Set; + +public interface UnweightGraph extends Iterable { + + // FATTO + public boolean containsVertex(Vertex vertex); + + // FATTO + public boolean addVertex(Vertex vertex); + + // FATTO + public boolean removeVertex(Vertex vertex); + + // FATTO + public int edgeSize(); + + // FATTO + public boolean containsEdge(Vertex v1, Vertex v2); + + // FATTO + public boolean addEdge(Vertex v1, Vertex v2); + + // FATTO + public boolean removeEdge(Vertex v1, Vertex v2); + + // FATTO + public boolean isEmpty(); + + // FATTO + public int size(); + + // FATTO + public Set neighbors (Vertex v); + + // FATTO + public Iterator iterator(); + + // FATTO + public Iterator breadthFirstIterator (Vertex v); + + + public Iterator depthFisrtIterator (Vertex v); + +} diff --git a/asdl/src/network/network/Network.java b/asdl/src/network/network/Network.java new file mode 100644 index 0000000..fc84258 --- /dev/null +++ b/asdl/src/network/network/Network.java @@ -0,0 +1,311 @@ +package network.network; + +import network.Graph; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Map; +import java.util.Queue; +import java.util.Set; +import java.util.Stack; +import java.util.TreeMap; +import java.util.TreeSet; + +public class Network> implements Graph{ + + protected TreeMap> adjacencyMap; + + public Network() { + this.adjacencyMap = new TreeMap>(); + } + + public Network(Network network) { + this.adjacencyMap = new TreeMap>(network.adjacencyMap); + } + + @Override + public boolean equals(Object object) { + if (object == null) return false; + if (object == this) return true; + if (!(object instanceof Network)) return false; + + Network network = (Network) object; + + return adjacencyMap.equals(network.adjacencyMap); + } + + @Override + public boolean containsVertex(Vertex vertex) { + return adjacencyMap.containsKey(vertex); + } + + @Override + public boolean addVertex(Vertex vertex) { + // Null Check + if (vertex == null) throw new NullPointerException(); + // Se l'elemento è già presente + if (containsVertex(vertex)) return false; + // Inserimento + adjacencyMap.put(vertex, new TreeMap()); + return true; + } + + @Override + public boolean removeVertex(Vertex vertex) { + // Null check + if (vertex == null) throw new NullPointerException(); + // Se l'elemento non è presente + if (!containsVertex(vertex)) return false; + // Rimozione del vertice dalle liste di adiacenza + for (Vertex v : adjacencyMap.keySet()) { + adjacencyMap.get(v).remove(vertex); + } + // Rimozione del vertice dalle chiavi + adjacencyMap.remove(vertex); + return true; + } + + /* + * Restituisce il numero di archi presenti all'interno del grafo + */ + @Override + public int edgeSize() { + // Variabile contatore + int edges = 0; + // Conto degli archi + for (Vertex v : adjacencyMap.keySet()) { + edges += adjacencyMap.get(v).size(); + } + return edges; + } + + @Override + public Double getEdgeWheight(Vertex v1, Vertex v2) { + // Null check + if (v1 == null || v2 == null) throw new NullPointerException(); + // Verifichiamo se ambedue sono presenti nel grafo, se non ci sono allora return null. + if (!(containsVertex(v1) && containsVertex(v2))) return null; + // Otteniamo la lista di adiacenza + Map tmp = adjacencyMap.get(v1); + // Se il vertex2 non presenta il vertice2 allora non esiste il peso + if (!tmp.containsKey(v2)) return null; + return tmp.get(v2); + } + + @Override + public boolean containsEdge(Vertex v1, Vertex v2) { + // Null check + if (v1 == null || v2 == null) throw new NullPointerException(); + // Verifichiamo che ambedue siano nel grafico + if (!(containsVertex(v1) && containsVertex(v2))) return false; + // Otteniamo la lista di adiacenza + Map tmp = adjacencyMap.get(v1); + return tmp.containsKey(v2); + } + + @Override + public boolean addEdge(Vertex v1, Vertex v2, Double weight) { + // Null check + if (v1 == null || v2 == null || weight == null) throw new NullPointerException(); + // Verifichiamo che ambedue siano nel grafico + if (!(containsVertex(v1) && containsVertex(v2))) return false; + // Verifichiamo che non esista l'arco. Se esiste allora non fa nulla. Se non esiste lo aggiunge. + if (containsEdge(v1, v2)) return false; + // Aggiunta dell'edge + Map tmp = adjacencyMap.get(v1); + tmp.put(v2, weight); + return true; + } + + @Override + public boolean removeEdge(Vertex v1, Vertex v2) { + // Null check + if (v1 == null || v2 == null) throw new NullPointerException(); + // Verifichiamo che ambedue siano nel grafico + if (!(containsVertex(v1) && containsVertex(v2))) return false; + // Verifichiamo se l'arco esiste oppure no. + if (!containsEdge(v1, v2)) return false; + // Rimozione dell'edge + Map tmp = adjacencyMap.get(v1); + tmp.remove(v2); + return true; + } + + @Override + public boolean isEmpty() { + return adjacencyMap.isEmpty(); + } + + @Override + public int size() { + return adjacencyMap.keySet().size(); + } + + @Override + public Set neighbors(Vertex v) { + // Null check + if (v == null) throw new NullPointerException(); + // Presenza del vertice nel grafo + if (!containsVertex(v)) return null; + // Se il vertice è presente, allora restituisco il tutto + return new TreeSet(adjacencyMap.get(v).keySet()); + } + + @Override + public Iterator iterator() { + return new NetworkIterator(); + } + + protected class NetworkIterator implements Iterator { + + // Variabili + protected Iterator iterator; + protected Vertex currentVertex; + + // Costruttore + public NetworkIterator() { + this.iterator = adjacencyMap.keySet().iterator(); + } + + // hasNext + public boolean hasNext() { + return this.iterator.hasNext(); + } + + // next + public Vertex next() { + this.currentVertex = iterator.next(); + return this.currentVertex; + } + + // remove + public void remove() { + throw new UnsupportedOperationException(); + } + + } + + @Override + public Iterator breadthFirstIterator(Vertex vertex) { + if (!this.adjacencyMap.containsKey(vertex)) throw new IllegalArgumentException(); + return new BreadthFirstIterator(vertex); + } + + protected class BreadthFirstIterator implements Iterator { + + // Variabili + protected Queue queue; + protected HashMap research; + protected Vertex currentVertex; + + // Costruttore + public BreadthFirstIterator(Vertex vertex) { + this.queue = new LinkedList(); + this.research = new HashMap(); + + for (Vertex v : adjacencyMap.keySet()) { + research.put(v, false); + } + + queue.add(vertex); + research.put(vertex, true); + } + + public boolean hasNext() { + return !(queue.isEmpty()); + } + + public Vertex next() { + currentVertex = queue.remove(); + + Set neighborsSet = adjacencyMap.get(currentVertex).keySet(); + + for (Vertex v : neighborsSet) { + if (research.get(v)) continue; + research.put(v, true); + queue.add(v); + } + + return currentVertex; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + + } + + @Override + public Iterator depthFisrtIterator(Vertex v) { + return new DepthFirstIterator(v); + } + + protected class DepthFirstIterator implements Iterator{ + + // Variabili + protected Stack stack = new Stack(); + protected HashMap reached; + Vertex current; + + // Costruttore + public DepthFirstIterator(Vertex vertex) { + this.stack = new Stack(); + this.reached = new HashMap(); + + for (Vertex v : adjacencyMap.keySet()) { + this.reached.put(v, false); + } + + this.stack.add(vertex); + this.reached.put(vertex, true); + } + + public boolean hasNext() { + return !stack.isEmpty(); + } + + public Vertex next() { + this.current = this.stack.pop(); + + Set neighborsSet = adjacencyMap.get(this.current).keySet(); + for (Vertex v : neighborsSet) { + if (this.reached.get(v)) { + if (this.stack.remove(v)) stack.add(v); + } else { + this.stack.add(v); + this.reached.put(v, true); + } + } + + return current; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + + } + + // ESERCIZI + + /* + * Un grafo è detto fortementeConnesso quando per ogni vertice + * si può raggiungere un qualsiasi altro vertice del grafo + */ + public boolean isStronglyConnected() { + for (Vertex SS : adjacencyMap.keySet()) { + Iterator it = this.breadthFirstIterator(SS); + int counter = 0; + while (it.hasNext()) { + counter++; + it.next(); + } + if (counter < adjacencyMap.size()) return false; + } + return true; + } + + + +} diff --git a/asdl/src/network/network/UnweightNetwork.java b/asdl/src/network/network/UnweightNetwork.java new file mode 100644 index 0000000..c09681a --- /dev/null +++ b/asdl/src/network/network/UnweightNetwork.java @@ -0,0 +1,277 @@ +package network.network; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Set; +import java.util.Stack; + +import network.UnweightGraph; + +public class UnweightNetwork implements UnweightGraph { + + // Lista di adiacenza + private HashMap> adjacencyMap; + + // Costruttore + public UnweightNetwork() { + adjacencyMap = new HashMap>(); + } + + public UnweightNetwork(UnweightNetwork network) { + adjacencyMap = new HashMap>(network.adjacencyMap); + } + + @Override + public boolean equals(Object object) { + if (object == null) return false; + if (object == this) return true; + if (!(object instanceof UnweightNetwork)) return false; + + UnweightNetwork unweightNetwork = (UnweightNetwork) object; + + return this.adjacencyMap.equals(unweightNetwork.adjacencyMap); + } + + @Override + public boolean isEmpty() { + return this.adjacencyMap.isEmpty(); + } + + @Override + public int size() { + return this.adjacencyMap.size(); + } + + @Override + public int edgeSize() { + int counter = 0; + for (Vertex v : adjacencyMap.keySet()) { + HashSet neighbours = adjacencyMap.get(v); + counter += neighbours.size(); + } + return counter; + } + + @Override + public boolean containsVertex(Vertex vertex) { + return adjacencyMap.containsKey(vertex); + } + + @Override + public boolean addVertex(Vertex vertex) { + // Null check + if (vertex == null) throw new NullPointerException(); + // Controllo la presenza di un vertice già presente all'interno del grafo + if (containsVertex(vertex)) return false; + // Aggiunta di un vertice e della sua lista di adiacenza + HashSet vertexAdjacencyMap = new HashSet(); + adjacencyMap.put(vertex, vertexAdjacencyMap); + return true; + } + + @Override + public boolean removeVertex(Vertex vertex) { + // Null check + if (vertex == null) throw new NullPointerException(); + // Controllo la presenza di un vertice già presente + if (!(containsVertex(vertex))) return false; + // Rimozione del vertice stesso + adjacencyMap.remove(vertex); + // Rimozione del vertice in altre liste di adiacenza + for (Vertex v : adjacencyMap.keySet()) { + HashSet neighboorsAdjacencyMap = adjacencyMap.get(v); + neighboorsAdjacencyMap.remove(vertex); + } + return true; + } + + @Override + public boolean containsEdge(Vertex v1, Vertex v2) { + // Null check + if (v1 == null || v2 == null) throw new NullPointerException(); + // Contorllo la presenza dei vertici che siano già esistenti. + if (!((containsVertex(v1)) && (containsVertex(v2)))) return false; + // Data la presenza dei due vertici continua pure il tutto. + // Acquisiamo la lista di adiacenza del vertice 1 + HashSet neighboorsAdjacencyMap = adjacencyMap.get(v1); + // Controlliamo che sia presente v2 nella lista di adiacenza di V1 + return neighboorsAdjacencyMap.contains(v2); + } + + @Override + public boolean removeEdge(Vertex v1, Vertex v2) { + // Null check + if (v1 == null || v2 == null) throw new NullPointerException(); + // Contorllo la presenza dei vertici che siano già esistenti. + if (!((containsVertex(v1)) && (containsVertex(v2)))) return false; + // Data la presenza dei due vertici continua pure il tutto. + // Acquisiamo la lista di adiacenza del vertice 1 + HashSet neighboorsAdjacencyMap = adjacencyMap.get(v1); + // Controlliamo che sia presente v2 nella lista di adiacenza di V1 + if (!neighboorsAdjacencyMap.contains(v2)) return false; // V2 non è contenuto nella lista di adicenza di v2 pertanto non può essere rimosso perchè l'arco non esiste + // Data la presenza del vertice in v2, procediamo alla rimozione di v2. + neighboorsAdjacencyMap.remove(v2); + return true; + } + + @Override + public boolean addEdge(Vertex v1, Vertex v2) { + // Null check + if (v1 == null || v2 == null) throw new NullPointerException(); + // Controllo la presenza dei due vertici + if (!(containsVertex(v1) && containsVertex(v2))) return false; + // Data la presenza dei due vertici continua ad aggiungere l'arco tra i due vertici + // Arco v1 -> v2, quindi v2 deve essere aggiunto dentro alla lista di adiacenza di v1 + HashSet neighboorsAdjacencyMap = adjacencyMap.get(v1); + return neighboorsAdjacencyMap.add(v2); + } + + @Override + public Set neighbors (Vertex v){ + // Null check + if (v == null) throw new NullPointerException(); + // Verifichiamo che il vertice esista + if (!(containsVertex(v))) return null; + // Data la presenza di v1, si restituisca la lista di adiacenza + return adjacencyMap.get(v); + } + + @Override + public Iterator iterator(){ + return new UnweightNetworkIterator(); + } + + protected class UnweightNetworkIterator implements Iterator { + + // VARIABILI + private Vertex currentVertex; + private Iterator iterator; + + // Costruttore + public UnweightNetworkIterator() { + iterator = adjacencyMap.keySet().iterator(); + } + + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + public Vertex next() { + currentVertex = iterator.next(); + return currentVertex; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + + } + + @Override + public Iterator breadthFirstIterator (Vertex vertex){ + if (!this.adjacencyMap.containsKey(vertex)) throw new IllegalArgumentException(); + return new BreadthFirstIterator(vertex); + } + + protected class BreadthFirstIterator implements Iterator { + + // Variabili + protected Queue queue; + protected HashMap research; + protected Vertex currentVertex; + + // Costruttore + public BreadthFirstIterator(Vertex vertex) { + this.queue = new LinkedList(); + this.research = new HashMap(); + + for (Vertex v : adjacencyMap.keySet()) { + this.research.put(v, false); + } + + this.queue.add(vertex); + this.research.put(vertex, true); + } + + public boolean hasNext() { + return !this.queue.isEmpty(); + } + + public Vertex next() { + + currentVertex = this.queue.remove(); + + Set neighborsSet = adjacencyMap.get(currentVertex); + + for (Vertex v : neighborsSet) { + if (this.research.get(v)) continue; + this.research.put(v, true); + queue.add(v); + } + + return currentVertex; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + + } + + public Iterator depthFisrtIterator (Vertex v) { + if (!this.adjacencyMap.containsKey(v)) throw new IllegalArgumentException(); + return null; //TODO + } + + protected class DepthFirstIterator implements Iterator{ + + // Variabili + protected Stack stack = new Stack(); + protected HashMap reached; + Vertex current; + + // Costruttore + public DepthFirstIterator(Vertex vertex) { + this.stack = new Stack(); + this.reached = new HashMap(); + + for (Vertex v : adjacencyMap.keySet()) { + this.reached.put(v, false); + } + + this.stack.add(vertex); + this.reached.put(vertex, true); + } + + public boolean hasNext() { + return !stack.isEmpty(); + } + + public Vertex next() { + this.current = this.stack.pop(); + + Set neighborsSet = adjacencyMap.get(this.current); + for (Vertex v : neighborsSet) { + if (this.reached.get(v)) { + if (this.stack.remove(v)) stack.add(v); + } else { + this.stack.add(v); + this.reached.put(v, true); + } + } + + return current; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + + } + +} diff --git a/asdl/src/network/undirected_network/UndirectedNetwork.java b/asdl/src/network/undirected_network/UndirectedNetwork.java new file mode 100644 index 0000000..08814bc --- /dev/null +++ b/asdl/src/network/undirected_network/UndirectedNetwork.java @@ -0,0 +1,48 @@ +package network.undirected_network; + +import java.util.Iterator; + +import network.network.Network; + +public class UndirectedNetwork> extends Network { + + public UndirectedNetwork() { + super(); + } + + public UndirectedNetwork(UndirectedNetwork network) { + super(network); + } + + public boolean equals(Object object) { + if (object == null) return false; + if (object == this) return true; + if (!(object instanceof UndirectedNetwork)) return false; + + UndirectedNetwork undirectedNetwork = (UndirectedNetwork) object; + + return this.adjacencyMap.equals(undirectedNetwork.adjacencyMap); + } + + @Override + public boolean addEdge(Vertex v1, Vertex v2, Double weight) { + return super.addEdge(v1, v2, weight) && super.addEdge(v2, v1, weight); + } + + @Override + public boolean removeEdge(Vertex v1, Vertex v2) { + return super.removeEdge(v1, v2) && super.removeEdge(v2, v1); + } + + public boolean isConnected() { + Vertex v = adjacencyMap.firstKey(); + Iterator it = new BreadthFirstIterator(v); + int counter = 0; + while (it.hasNext()) counter++; + if (counter < adjacencyMap.size()) return false; + return true; + } + + + +} diff --git a/asdl/src/parziale/p231110/Esercizio.java b/asdl/src/parziale/p231110/Esercizio.java new file mode 100644 index 0000000..e19538c --- /dev/null +++ b/asdl/src/parziale/p231110/Esercizio.java @@ -0,0 +1,35 @@ +package parziale.p231110; + +import java.util.Map; +import java.util.HashMap; + +public class Esercizio { + + /* + * Scrivere un metodo generico statico che prende in input + * un array di elementi e restituisce il + * numero di oggetti presenti all’interno dell’array che + * non presentano duplicazioni. Quindi + * utilizzare il metodo sul tipo Integer. + * Esempio: se l’array fosse {10, 6, 5, 6, 7, 8, 6, 8, 10, 12, 10}, + * il metodo dovrebbe restituire + * l’intero 3 (in grassetto i 3 elementi che non presentano duplicazioni). + */ + + public static int numeroOccorrenzeSingole(int[] array) { + if (array == null) throw new NullPointerException(); + Map lista = new HashMap(); + for (Integer i : array) { + if (lista.get(i) == null) lista.put(i, 1); + else lista.put(i, lista.get(i) + 1); + } + + int counter = 0; + for (Integer i : lista.keySet()) { + if (lista.get(i) == 1) counter++; + } + + return counter; + } + +} diff --git a/asdl/src/parziale/p231110/Libreria.java b/asdl/src/parziale/p231110/Libreria.java new file mode 100644 index 0000000..074b989 --- /dev/null +++ b/asdl/src/parziale/p231110/Libreria.java @@ -0,0 +1,116 @@ +package parziale.p231110; + +import java.lang.Integer; + +import java.util.Comparator; + +import java.util.Collections; + +import java.util.List; +import java.util.ArrayList; + +import java.util.Set; +import java.util.HashSet; + +import java.util.Map; +import java.util.TreeMap; + +public class Libreria { + + /* + * Libreria che rappresenta una libreria + * come una lista di libri senza duplicati e + * supporta le seguenti operazioni. + */ + + Set libreria = new HashSet(); + + /* + * Metodo n.1 + * inserire nella libreria un nuovo libro, se non è già presente, + * dati in input l’ISBN e l’anno + * di pubblicazione; + */ + + public boolean aggiungiLibro(Libro libro) { + if (libro == null) throw new NullPointerException(); + return libreria.add(libro); + } + + /* + * verificare se la libreria contiene un libro, + * dato in input il suo ISBN; + */ + + public boolean contieneLibro(String ISBN) { + return libreria.contains(new Libro(ISBN, 0)); + } + + /* + * cancellare dalla libreria un libro, se presente, + * dato in input il suo ISBN; + */ + + public boolean rimuoviLibro(String ISBN) { + return libreria.remove(new Libro(ISBN, 0)); + } + + /* + * ordinare la lista dei libri in ordine + * crescente rispetto al codice ISBN; + */ + + public List listaOrdinata() { + List listaOrdinata = new ArrayList(libreria); + if (libreria.isEmpty()) return listaOrdinata; + Collections.sort(listaOrdinata, new Comparator() { + @Override + public int compare(Libro l1, Libro l2) { + String ISBN1 = l1.ISBN(); + String ISBN2 = l2.ISBN(); + + return ISBN1.compareTo(ISBN2); + } + }); + return listaOrdinata; + } + + /* + * ordinare la lista dei libri in ordine decrescente + * rispetto all’anno di prima pubblicazione; + */ + + public List listaOrdinataPerAnnoDec() { + List listaOrdinata = new ArrayList(libreria); + if (libreria.isEmpty()) return listaOrdinata; + Collections.sort(listaOrdinata, new Comparator() { + @Override + public int compare(Libro l1, Libro l2) { + int anno1 = l1.annoPubblicazione(); + int anno2 = l2.annoPubblicazione(); + + return Integer.compare(anno2, anno1); + } + }); + return listaOrdinata; + } + + /* + * calcolare restituire in output, per ogni anno di + * pubblicazione compreso tra il 2000 e il + * 2023, il relativo numero di libri contenuti nella libreria. + */ + + public Map numeroLibriTra2000e2023() { + // Anno Libro (Integer), Numero di occorrenze (Integer) + Map mappaOrdinata = new TreeMap(); + for (Libro libro : libreria) { + int anno = libro.annoPubblicazione(); + if (!(anno >= 2000 && anno <= 2023)) continue; + if (mappaOrdinata.get(anno) == null) mappaOrdinata.put(anno, 1); + else mappaOrdinata.put(anno, 1 + mappaOrdinata.get(anno)); + } + return mappaOrdinata; + } + +} diff --git a/asdl/src/parziale/p231110/Libro.java b/asdl/src/parziale/p231110/Libro.java new file mode 100644 index 0000000..9cd1cdf --- /dev/null +++ b/asdl/src/parziale/p231110/Libro.java @@ -0,0 +1,45 @@ +package parziale.p231110; + +import java.util.Objects; + +public class Libro{ + + // Metodi di instanza + private String ISBN; + private int annoPubblicazione; + + // Metodo costruttore + public Libro( + String ISBN, + int annoPubblicazione) { + this.ISBN = ISBN; + this.annoPubblicazione = annoPubblicazione; + } + + // Metodi getter + public String ISBN() { + return ISBN; + } + + public int annoPubblicazione() { + return annoPubblicazione; + } + + // Metodi di oggetto (fondamentali per HashSet usato in Libreria) + @Override + public boolean equals(Object obj) { + if (obj == null) return false; + if (obj == this) return true; + if (!(obj instanceof Libro)) return false; + + Libro extObj = (Libro) obj; + + return this.ISBN.equals(extObj.ISBN); + } + + @Override + public int hashCode() { + return Objects.hash(ISBN); + } + +}